Rumah  >  Soal Jawab  >  teks badan

Cari lajur JSON Laravel dalam jadual lain melalui perkaitan

Saya cuba menulis kod carian dan ini adalah kod saya:

$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();

Tetapi apabila saya menghantar plan_name dalam permintaan, kod menapis mengikut nama perkhidmatan dan bukannya nama pelan

Saya cuba menapis data mengikut nama pelan apabila menghantar kod nama pelan dalam permintaan tetapi ia tidak berfungsi

P粉596161915P粉596161915259 hari yang lalu392

membalas semua(1)saya akan balas

  • P粉807471604

    P粉8074716042024-02-27 13:45:32

    if ($request->service_name && !$request->plan_name) { // When only service name provided
        $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 && !$request->service_name) { // When only plan name provided
        $plan_name = $request->plan_name;
        $services = $services->whereHas('plans', function ($q) use ($plan_name) {
            $q->where('name->en','Like','%'.$plan_name.'%');
        });
    }

    balas
    0
  • Batalbalas