20

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

1
  • try this one strtolower Commented Sep 15, 2017 at 11:43

3 Answers 3

43

Use LOWER()

$query->whereRaw('LOWER(`newsTitle`) LIKE ? ',[trim(strtolower($newsTitle)).'%']);
Sign up to request clarification or add additional context in comments.

1 Comment

still working today! I only added '%'. before trim to allow more wildcard searches
1

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;
    }

Comments

1

Use LOWER()

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

The above answer is right. But "trim" after using "strtolower" is faster working.

1 Comment

You mean "strtolower" after "trim" is faster? According to your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.