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

greatsami's avatar

return similar values in two arrays in Eloquent query

Hi all,

I have user_data table contain column sub_sectors and its value string like (2,3,4)

And I have another table called (orders) also contain column called sub_sectors and its value as json like:

[{"subsector":"2"},{"subsector":"3"},{"subsector":"4"},{"subsector":"5"},{"subsector":"6"},{"subsector":"7"},{"subsector":"8"},{"subsector":"9"},{"subsector":"10"},{"subsector":"11"},{"subsector":"12"},{"subsector":"13"},{"subsector":"14"},{"subsector":"15"},{"subsector":"16"},{"subsector":"17"},{"subsector":"18"},{"subsector":"19"},{"subsector":"20"},{"subsector":"21"},{"subsector":"22"},{"subsector":"23"}]

I want to make eloquent query that select all orders where order.sub_sectors matched user_data.sub_sectors.

In my example situation i need all orders have sub_sectors 2 or 3 or 4

How I can do that?

Thanks,

0 likes
1 reply
rumm.an's avatar

You can query JSON columns, see docs

$sectors = UserData::where( /*... Some Query... */ )->firstOrFail()->sub_sectors;
$sectors = explode(',', $sectors); //You can also write a regex if it is stored in a complex form.
Orders::whereIn('sub_sectors->subsector', $sectors)->get();

This could work, I'm not sure but you get the Idea!

Please or to participate in this conversation.