php - Laravel query a Model over one of it's related models -
i have model has related object. data have been stored in same model, resides in different database used , attach model.
say have following structure: basemodel->attachedmodel
i attached models criteria, via basemodel since there few more objects attached basemodel need.
i tried achieve using basemodel::where('attachedmodel.id', 100)->get()
(since looks with()
method hoped work)
in case i'm trying basemodels there attachedmodel id 100. fails.
can done, or should use raw query this?
the with() method eager load relation , not filter results. need wherehas query, filters results based on criteria on related model.
$lookingforid = 100; basemodel::wherehas('attachedmodels', function ($query) use ($lookingforid) { $query->where('id', '=', $lookingforid); })->get();
wherehas() first parameter (in above code 'attachedmodels'), should name of relation, defined model class
Comments
Post a Comment