luayjal's avatar

laravel search in other table on json column by relation

i try make code for search and this is my code:

$services = Service::query()->with('plans')->latest();

	if ($request->service_name) {
               $services = $services->whereRaw("CONVERT(JSON_EXTRACT(name, '$.ar') using 'utf8') LIKE  '%$request->service_name%' ")
                ->orWhereRaw("CONVERT(JSON_EXTRACT(name, '$.en') using 'utf8') LIKE  '%$request->service_name%' ")
                ->orWhereRaw("CONVERT(JSON_EXTRACT(name, '$.he') using 'utf8') LIKE  '%$request->service_name%' ");		
    }

        if ($request->plan_name) {
            $plan_name = $request->plan_name;
            $services = $services->whereHas('plans', function ($q) use ($plan_name) {
                $q->where('name->en','Like','%'.$plan_name.'%');
            });
       }

        return $services->get();

but when i send plan_name in request the code filter by service name and not filter by plan name

0 likes
9 replies
s4muel's avatar

do you have some kind of debugger to show us what sql queries were executed?

or use DB::enableQueryLog() to turn on query log

DB::enableQueryLog();

if ($request->service_name) {
    $services = $services->whereRaw("CONVERT(JSON_EXTRACT(name, '$.ar') using 'utf8') LIKE  '%$request->service_name%' ")
        ->orWhereRaw("CONVERT(JSON_EXTRACT(name, '$.en') using 'utf8') LIKE  '%$request->service_name%' ")
        ->orWhereRaw("CONVERT(JSON_EXTRACT(name, '$.he') using 'utf8') LIKE  '%$request->service_name%' ");		
}

if ($request->plan_name) {
    $plan_name = $request->plan_name;
    $services = $services->whereHas('plans', function ($q) use ($plan_name) {
        $q->where('name->en','Like','%'.$plan_name.'%');
    });
}

dd(DB::getQueryLog());

and show us the dump.

or just $services->ddRawSql() if you're on L10 might work too

luayjal's avatar

@s4muel i have this result array:1 [ 0 => array:3 [ "query" => "select * from services where exists (select * from services as laravel_reserved_0 inner join plan_services on laravel_reserved_0.id = plan_services.service_id where services.id = plan_services.service_id and json_unquote(json_extract(name, '$."en"')) Like ? and laravel_reserved_0.deleted_at is null) and services.deleted_at is null order by created_at desc" "bindings" => array:1 [ 0 => "%تسلا%" ] "time" => 1.0 ] ]

s4muel's avatar

@luayjal although it shouldn't be like i am going to advise, but i am curious anyway: try specifying the table (plan_services.name->en) in the innermost where

if ($request->plan_name) {
    $plan_name = $request->plan_name;
    $services = $services->whereHas('plans', function ($q) use ($plan_name) {
        $q->where('plan_services.name->en','like','%'.$plan_name.'%');
    });
}

and give me again the sql dump

krisi_gjika's avatar

since you are using OR conditions in your query, try wrapping them in a single condition:

$services->where(function ($query) use ($request) {
  $query->whereRaw("CONVERT(JSON_EXTRACT(name, '$.ar') using 'utf8') LIKE  '%$request->service_name%' ")
        ->orWhereRaw("CONVERT(JSON_EXTRACT(name, '$.en') using 'utf8') LIKE  '%$request->service_name%' ")
        ->orWhereRaw("CONVERT(JSON_EXTRACT(name, '$.he') using 'utf8') LIKE  '%$request->service_name%' ");
);		
1 like
luayjal's avatar

@krisi_gjika The problem is in this condition if ($request->plan_name) { $plan_name = $request->plan_name; $services = $services->whereHas('plans', function ($q) use ($plan_name) { $q->where('name->en','Like','%'.$plan_name.'%'); }); }

krisi_gjika's avatar

@luayjal where is this name column coming from, services or plans?

Also try replacing your ->where('name->en','Like','%'.$plan_name.'%'); with ->whereRaw("CONVERT(JSON_EXTRACT(name, '$.en') using 'utf8') LIKE '%$plan_name%' ") like you have done for service_name

luayjal's avatar

@krisi_gjika i try whereRaw("CONVERT(JSON_EXTRACT(name, '$.en') using 'utf8') LIKE '%$plan_name%' ") and no result

jlrdw's avatar

And I'll add, storing Json is not a good idea, the forum has so many questions on dealing with it. It really becomes a mess. Just my opinion.

Setup correct table structure like @tray2 has said countless times on the forum.

1 like

Please or to participate in this conversation.