Home > other >  Laravel Eloquent: Query returns no records but SQL does
Laravel Eloquent: Query returns no records but SQL does

Time:07-20

I have this query:

$query = City::query();

    $query->select('id', 'name as default_name', 'translations->' . $request["lang"] . ' as name', 'country_id');

    $query->where(function ($query) use ($request) {
        $query->whereRaw('LOWER(translations->"$.' . $request["lang"] . '") like ?', "'%" . strtolower($request['search']) . "%'");
        $query->orWhere('name', 'like', "'%" . strtolower($request->search) . "%'");
    });

    if ($request->country !== null) {
        $query->whereRaw('country_id = '. $country);
    }

    $results = $query->get();

That translates to this SQL:

select `id`, `name` as `default_name`, json_unquote(json_extract(`translations`, '$."en"')) as `name`, `country_id` 
from `cities` 
where (LOWER(translations->"$.en") like '           
  • Related