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
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.
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.
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.
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.
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.
I suggest Upper function in this case
Model::whereRaw("UPPER('{$column}') LIKE '%'". strtoupper($value)."'%'");
like this
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();
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
This should work.
Replace column
and $text
.
$model = Models::whereRaw( 'LOWER(`column`) LIKE ?', [ $text ] )->first();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With