Key point: Closure
Model:
public function getCollect() { return $this->belongsTo('App\Components\Misc\Models\CollectCareerTalk', 'id', 'career_talk_id'); } public function otherMethod() { return $this->belongsTo('App\Components\Misc\Models\OtherMethodModel', 'Primary Key', 'Foreign Key'); }
storehouse:
$this->model->with(['getCollect' => function ($q) use ($user_id) { $q->where('user_id', $user_id); }, 'otherMethod']) ->select('id', 'title') ->where([ 'id' => 1 ]) ->first();
Notice:With can connect multiple tables (transfer parameters in array form). If there are no dynamic conditions, you can directly write the model method name into the with parameters. If there are dynamic conditions, write it into the closure.
The above article using with in laravel to dynamically add where conditions is all the content I share with you. I hope you can give you a reference and I hope you support me more.