You can define several methods for one relation filtered as you like, for example:
public function photos() {
return $this->hasMany('Photo');
}
public function photosDefault() {
return $this->hasMany('Photo')->where('default','1');
}
public function photosRecent() {
return $this->hasMany('Photo')->where('created_at', '>', Carbon\Carbon::now()->subWeek()->toDateTimeString();
}
Otherwise use photos()->...->get() as it returns Relation object not Eloquent Model/Collection so you can chain it.
And by the way you can easily do it on the fly when eager loading the model:
User::with(['photos' => function ($query) {
$query->where('default', '1');
}])->get();
Thanks so much for the reply - will test it tomorrow and report back!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community