I have this problem with Laravel Eloquent. I have two models - for simplicity's sake named:
- A (id, name)
- B (id, a_id, created_at)
- relationship: A hasMany B
I need to return all B records filtered by these conditions:
- A.name = given_name
- B.created_at >= given_date
I want to do this by passing in a closure. I searched through the laravel documentation on models:
https://laravel.com/docs/7.x/eloquent
https://laravel.com/docs/7.x/eloquent-relationships
and found these examples, but how do I put this together?
public function user()
{
return $this->belongsTo('App\User')->withDefault(function ($user, $post) {
$user->name = 'Guest Author';
});
}
function (Builder $builder) {
$builder->where('age', '>', 200);
}