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

Engr khayam khan's avatar

How to filter a field using json_decode function in Laravel Query?

I want to filter child records with conditions if an element is present like in_array(someElement, json_decode(database_column)) but don't know exactly what will be the syntax. I have done it through loops but I want to clear that loops and wrap it up in one query.

Till now I have done the following,

$projects = Project::with([
    'tasks' => function($query) use ($request) {
        $query->when($request->input('region_id') !== 'All', function ($query) use ($request) {
            $query->where('region_id', $request->input('region_id'));
        })->when($request->input('taskowner_id') !== 'All', function ($query)  {
            
        });
    }
])->get(['id', 'project_name', 'customer_id']);

i want to insert the following line to that when the condition of task_owner_id

if (in_array($taskOwnerName->emp_name, json_decode($task->task_owner, false), true)) {
    
}
0 likes
3 replies

Please or to participate in this conversation.