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?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire