Get a collection from child relationship? Hello all,
I currently have 3 models, a Client, Venue and an Entry.
A Client hasMany Venue.
A Venue hasMany Entries.
On the client, I want to be able to fetch all entries for this.
I currently have this very sloppy way of doing it.
$entries = Collection::make();
foreach($this->venues as $venue) {
$entries->push($venue->entries);
}
return $entries;
How can I clean this up and allow it to be paginated?
Thanks!
Okay, using whereIn is working perfectly.
/**
* Get the Clients Entries
*
* @return Collection
*/
public function entries()
{
$ids = $this->venues->lists('id');
$entries = Entry::whereIn('venue_id', $ids);
return $entries;
}
Allows me to do.
$entries = Client::findOrFail(1)->entries()->paginate(20);
@JoeDawson You can load venues and entries with lazy eager load like this..
$client->load('venues.entries');
Which model do you want to paginate?
Perfect @SaeedPrez - thanks! Have been using Laravel for a couple of years, still learn more every day :)
/**
* Get the Clients Entries
*
* @return Collection
*/
public function entries()
{
return $this->hasManyThrough('App\Entry', 'App\Venue')->with('venue', 'form');
}
Please sign in or create an account to participate in this conversation.