Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

The first parameter in the Book::where method should be the column name. For example author_id instead of author->id. Given that a publisher can have many authors a query could look like this:

Book::where('author_id', $author->id)
    ->whereIn('author_id', function(Builder $query) {
        $query->select('id')->from('authors')->where('publisher_id', $pub->id);
    }
)->get();

This first selects all books by the given author. Then, in the whereIn method, it adds the constraint that the author also must belong to the given publisher.

Let me know if that works!

Last updated 2 years ago.
0

It doesn't work. (I think you modified the code, I will try it).

Last updated 2 years ago.
0

You will need a way to keep track of which books belongs to author X.

Your database structure would look like this

Table: books
====================
ID: 1
title: "Some awesome book"
author_id: 1
publisher_id: 1
---------------------------------------
ID: 2
title: "Laravel for beginners"
author_id: 2
publisher_id: 2


Table: authors
====================
ID: 1
name: "John Doe"
--------------------------------------
ID: 2
name: "Jenny Doe"



Table: publishers
====================
ID: 1
Name: "Publisher 1"
---------------------------------------
ID: 2
Name: "Publisher 2"

Let's say we wan't all books that are written by "John Doe" and published by "Publisher 1", our query would look like:

$books = Book::where('author_id', '=', 1)->where('publisher_id', '=', 1)->get();

Last updated 2 years ago.
0

So you recommend to add a publisher_id field to the books table.

But, every book has only one author, and every author has only one publisher. Isn't there a way to find the publishers of some books without adding a publisher field?

Will an extra publisher field in the books table increase the performance of the query?

Thanks..

Last updated 2 years ago.
0

I'm pretty sure books from the same author could have different publishers (In real life). So why limit your system to only 1 publisher per author? What you are trying to achieve is pretty easy with the Database structure in my previous reply.

To make your code i little bit more beautiful, i would make the Book model like this:

class Book extends Eloquent{

    public function author(){
        return $this->belongsTo('Author', 'author_id', 'id');
    }

    public function publisher(){
        return $this->belongsTo('Publisher', 'publisher_id', 'id');
    }

}

And then use it like this:


$book = Book::with( array( 'author', 'publisher') )->find(1);

echo $book->author->name; // Outputs: "John Doe"
echo $book->publisher->name; // Outputs: "Publisher 1" 

Last updated 2 years ago.
0

You are right, authors can have different publishers. But I just made an anology with the publisher-author-book model to make it easy to express my problem. My real models have different names. And in my model, each "author" has only one "publisher".
So, I want to repeat my question: Will an extra publisher field in the books table increase the performance of the query?

Last updated 2 years ago.
0

Fatihir, try the query I wrote again and see if that works. It should!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

mehmet mehmet Joined 18 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.