php - Laravel eloquent query issue -
i trying users images relation 1 many. in images table column type equal 1.
closest created.
user::with('images')->has('images')->get();
but return images not type 1;
also tried
$a = user::defaultimage()->get();
and in user model def scope
public function scopedefaultimage($query) { $query->wherehas('images', function ($query) { $query->where('default', 1); })->get(); return $query; }
but doesn't return relation images, user
you want modify way using with
function. make images returned of type 1.
user::with(['images' => function($q) { $q->where('type', 1); }])->wherehas('images', function($q) { $q->where('type', 1); })->get();
by adding wherehas
, return users have image type of 1 attached.
i make sure in database have users , attached images image type of 1 know sure should getting data back.
adding additional constraints, chain on wherehas
user::with(['images' => function($q) { $q->where('type', 1); }])->wherehas('images', function($q) { $q->where('type', 1); })->wherehas('roles', function($q) { $q->where('id', 5); })->get();
Comments
Post a Comment