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

Mqandeel's avatar

Fix error in Eloquent query

user

id name address_id

address

id photo_id

photo

id description

I need to get all user which has photo description ="nice" whats the correct Eloquent to get the result

$user= Users::with('address.photo', function (Builder $query) { $query->where('photo.description', 'like', 'nice'); } )->paginate(5);

0 likes
4 replies
bobbybouwmann's avatar

What is the error you get?

Anyway, your query should look like this

$user= Users::with('address.photo', function (Builder $query) { 
    $query->where('description', 'like', '%nice%'); 
})->paginate(5);
Mqandeel's avatar

i get error

ErrorException mb_strpos() expects parameter 1 to be string, object given

Snapey's avatar
Snapey
Best Answer
Level 122

I need to get all user which has photo description ="nice"

then you need to use whereHas to query by a relation.

Please or to participate in this conversation.