None
None

Reputation: 9247

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

Upvotes: 3

Views: 6796

Answers (4)

raffi
raffi

Reputation: 139

Here is an answer without using whereRaw:

use DB;

$data = BusinessUser::where(DB::raw('LOWER(company_name)'), 'like', '%' . strtolower($company_name) . '%');

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163948

You can use LOWER:

BusinessUser::whereRaw('LOWER(`company_name`) like ?', ['%'.strtolower($company_name).'%'])->pluck('company_name');

Upvotes: 6

zgabievi
zgabievi

Reputation: 1202

Your query should look like this:

$data = BusinessUser::whereRaw("company_name COLLATE UTF8_GENERAL_CI LIKE %{$company_name}%")->pluck('company_name');

Upvotes: 0

shukshin.ivan
shukshin.ivan

Reputation: 11340

Set an encoding for company_name to utf8_general_ci. CI means Case-Insensitive.

Upvotes: 0

Related Questions