Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check lowercase and uppercase search

I'm using this query:

  public function autocomplete(Request $request)
   {
      $company_name = $request->input('query');
      $data = BusinessUser::where("company_name","LIKE",'%'. $company_name .'%')->pluck('company_name');
      return response()->json($data);
   }

In database for company name i can have this: 'Test','TEST','test'. So how can i check all of this so that i get result. Any suggestion?

I tried this but then i get an error that i need to pass array:

  $data = BusinessUser::whereRaw("company_name","LIKE",'%'. $company_name .'%')->orWhereRaw("company_name","LIKE",'%'. $company_name .'%')->pluck('company_name');

EDIT:

I dont want to change anything in database

like image 812
None Avatar asked Nov 19 '25 03:11

None


2 Answers

You can use LOWER:

BusinessUser::whereRaw('LOWER(`company_name`) like ?', ['%'.strtolower($company_name).'%'])->pluck('company_name');
like image 192
Alexey Mezenin Avatar answered Nov 21 '25 09:11

Alexey Mezenin


Here is an answer without using whereRaw:

use DB;

$data = BusinessUser::where(DB::raw('LOWER(company_name)'), 'like', '%' . strtolower($company_name) . '%');
like image 35
raffi Avatar answered Nov 21 '25 08:11

raffi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!