Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search case insensitive in Eloquent model

I want to search case-insensitive in Eloquent model.

Now I am using this

Model::where($column, 'LIKE', '%' . $value . '%' ); 

But it is case sensitive. How can I solve this?

I also find this post How can I search (case-insensitive) in a column using LIKE wildcard? but I can not use it in Eloquent model

like image 317
Davit Zeynalyan Avatar asked Jul 24 '18 11:07

Davit Zeynalyan


People also ask

How do you make a search case-insensitive?

Case insensitive SQL SELECT: Use upper or lower functions select * from users where lower(first_name) = 'fred'; As you can see, the pattern is to make the field you're searching into uppercase or lowercase, and then make your search string also be uppercase or lowercase to match the SQL function you've used.

Is laravel case sensitive?

Laravel Routing Case-insensitive routeswill match a GET request to /login but will not match a GET request to /Login . In order to make your routes case-insensitive, you need to create a new validator class that will match requested URLs against defined routes.

How do I select case sensitive in MySQL?

It is important to note that MySql is not only case insensitive for columns using an _ci collation (which is typically the default), but also accent insensitive. This means that 'é' = 'e' . Using a binary collation (or the binary operator) will make string comparisons accent sensitive as well as case sensitive.

How do I make a case-insensitive in MySQL?

select * from users where lower(first_name) = 'ajay'; The method is to make the field you are searching as uppercase or lowercase then also make the search string uppercase or lowercase as per the SQL function.


5 Answers

Actually, you don't need to use UPPER, just use ilike as the comparator and it will do a case-insensitive comparison.

Model::where('column', 'ilike', '%' . $value . '%')

You do need the % signs to signify the substring you're searching for.

like image 53
Randy Dryburgh Avatar answered Oct 23 '22 10:10

Randy Dryburgh


I suggest Upper function in this case

Model::whereRaw("UPPER('{$column}') LIKE '%'". strtoupper($value)."'%'"); 

like this

like image 35
Ts8060 Avatar answered Oct 23 '22 11:10

Ts8060


Didn't see this solution. so I'm posting this here.

DB::table('products')
   ->select('productid')                
   ->where(DB::raw('lower(product)'), 'like', '%' . strtolower($searchword) . '%')
   ->get();
like image 12
Thanushka Avatar answered Oct 23 '22 11:10

Thanushka


Laravel eloquent search is case insensitive, you don't need ilike, upper or lower functions in your query.

Just use:

Controller::query()
    ->where('column1_name', 'LIKE', "%{$search}%") 
    ->orWhere('column2_name', 'LIKE', "%{$search}%") 
    ->orWhere('column3_name', 'LIKE', "%{$search}%") 
    ->get();

The orWhere part is in case you have more than one table field that you want to query

like image 5
Damilare Koiki Avatar answered Oct 23 '22 09:10

Damilare Koiki


This should work.

Replace column and $text.

$model = Models::whereRaw( 'LOWER(`column`) LIKE ?', [ $text ] )->first();
like image 6
Unicco Avatar answered Oct 23 '22 11:10

Unicco