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

devondahon's avatar

Query on relationships's json column

I'm trying to get users from their foo relationship, and from an id which is stored in a data json column. I tried the syntax below which is wrong :

User::with('foos')->where(['foos.data->id' => 123])->get()

What is the correct syntax for such query ?

0 likes
5 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You need to make the query on the with() query

User::with(['foos' => function($query)
    $query->where('data->id', 123);
})->get()
1 like
Tray2's avatar

The best option is not to use json columns.

2 likes

Please or to participate in this conversation.