Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jenya's avatar
Level 2

Related models in query filter

Hello, Laracasts! I try to make search by my related models, parameters to query filter I pass from search form inputs. Models are:

class Card extends Model
{
    public function Doer()
    { 
        return $this->hasMany('App\Doer,'card_id');
    }
}
class Doer extends Model
{
    public function Card()
    {
        return $this->belongsTo('App\Card');
    }
    public function City()
    {
        return $this->belongsTo('App\City','city_id');
    }

}

and

class City extends Model
{
     public function Doer()
    {
        return $this->hasMany('App\Doer','city_id');
    }
}

I want to select only Cards which number == $request->input('number') and doer only from city where city == $request->input('city').

I can get Cards with Doers:

$x=Card::with([ 'Doer'=>function($query)
                              {
                               //some conditions ???
                              }
                              ])->get( );

But how to select only doer which id = City.id and City.description ==$request->input('city')?

0 likes
0 replies

Please or to participate in this conversation.