laravel:how to use LOWERCASE Function in Eloquent Query?

Viewed 31927

I want to implement case insensitive search following is my code for search

/*get the title to search*/
$newsTitle=Input::get('srach_newsTitle')?Input::get('srach_newsTitle'):'';
$query = DB::table('news');
if( strlen($newsTitle) )
    {
        $query->whereRaw('LOWERCASE(`newsTitle`) LIKE ? ',[trim(strtolower($newsTitle)).'%']);
    }

but its says function LOWERCASE is not Defined in My projects following is the error message

QueryException in Connection.php line 729: SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION cpr-laravel.LOWERCASE does not exist (SQL: select count(*) as aggregate from news where LOWERCASE(newsTitle) LIKE test%)

whats wrong i am doing?plz help

2 Answers

The above code is good but has some problems. The main thing is that letters in Krill cannot be lowercase

Here's a sample that worked for me

$search = mb_strtolower(trim(request()->input('search')));

$query->whereRaw('LOWER(`newsTitle`) LIKE ? ',['%'.$search.'%']);

I wanted to search for data stored in multi-language JSON, and this worked for me.

public function search()
    {
        $search = mb_strtolower(trim(request()->input('search')));
        $products = DB::table('products')
            ->whereRaw("LOWER(JSON_EXTRACT(name, \"$.".app()->getLocale()."\")) LIKE ?", ["%".$search."%"])
            ->whereNull('deleted_at')
            ->select(['id', 'name', 'slug',])
            ->orderBy('id', 'desc')
            ->limit(15)->get();

        return $products;
    }
Related