Reputation: 621
Let's say we have two models Book and Author. The Book model defines a relationship to get the author:
public function author()
{
return $this->belongsTo(Author::class, 'author_id','id');
}
If we want to get all of the books that were published in 2000, along with their authors, we can do this:
$books = Book::with('author')->where('publication_year', 2000)->get();
Now what if we want to return the unique Authors who published in 2000 separately from their Books? Let's say Bob published three books and Sally published two books. We'd have one collection that included the 5 books without their authors, and another collection that included Bob and Sally once each.
This is the closest I've gotten (this is a simplified example and may contain typos):
$books = Book::with('author')->where('publication_year', 2000)->get();
$authors = $books->pluck('author')->unique('id');
//this part is bad:
foreach ($books as $book) {
unset($book['author']);
}
Is there a more efficient way to do this, or do I have to set up a manual JOIN?
Upvotes: 3
Views: 114
Reputation: 17688
You can use whereIn
as:
$books = Book::where('publication_year', 2000)->get();
$authors_id = $books->unique('author_id')->pluck('author_id');
$authors = Author::whereIn('id', $authors_id)->get();
Upvotes: 1
Reputation: 163948
If you want to get IDs of authors who have written books in 2000, you could use whereHas()
:
Author::whereHas('books', function($q) {
$q->where('publication_year', 2000);
})->pluck('id');
Upvotes: 2