Check lowercase and uppercase search - laravel

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

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

Here is an answer without using whereRaw:
use DB;
$data = BusinessUser::where(DB::raw('LOWER(company_name)'), 'like', '%' . strtolower($company_name) . '%');

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

Your query should look like this:
$data = BusinessUser::whereRaw("company_name COLLATE UTF8_GENERAL_CI LIKE %{$company_name}%")->pluck('company_name');

Related

How to fetch certain keyword's record in Laravel?

I would like to LIKE search.
If 'product' column has "AQ" string display data.
I wrote below code but I couldn't get any record.
Could you teach me right code please?
public function w_mo_fb_m()
{
$word = "AQ";
$images = ImageGallery::where('product', 'like', "%$word%")->orderBy('id', 'desc')->get();
return view('w_mo_fb_m',compact('images'));
}
How about try this.
where('product', 'LIKE', '%'.$word.'%')
//or
where('product', 'LIKE', "%{$word}%")
If this still not working you may need to run raw SQL to see if you can get any record. (you need to replace ImageGallery with your actual table name)
DB::select('select * from ImageGallery where product LIKE = ?', ['%'.$word.'%']);

Laravel eloquent using loop

I am using eloquent query to filter data. My query is like this and it is working perfectly for now.
$users = User::where('username','LIKE','%'.request('username').'%')
->where('first_name','LIKE','%'.request('first_name').'%')
->where('last_name','LIKE','%'.request('last_name').'%')
->where('gender','LIKE','%'.request('gender').'%')
->where('password','LIKE','%'.request('password').'%')
->SimplePaginate(15);
My request data was like this.
However I need to update this query for dynamic fields. There can be different fields. What I did was to send the request in an associative array. So, my request data turned into like this,
What I intend to do is to put all request data in a search array. Then use a loop to run like the above query. How can I do that?
P.S. I checked in the forum and found some similar questions. Some of them are outdated. Others didn't solve my problem.
If I understand you right you can do like this:
$search = request('search', []);
$users = User::query();
foreach($search as $field=>$value)
{
$users = $users->where($field,'LIKE','%'.$value.'%');
}
$users = $users->SimplePaginate(15);
You can try this.
$search = $request->get('search');
User::where(function ($q) use ($search){
foreach ($search as $key=>$value) {
if($value != null ){
$q->where($key, 'like', "%{$value}%");
}
}
})->get();
The where() clause can also accept an array. With that in mind, you could map over your search parameters and convert it to the format you want and then feed it into the query.
For example:
$search = [
'first_name' => 'John',
'last_name' => 'Smith'
];
$filters = collect($search)->map(function($value, $key) {
return [$key, 'LIKE', "%{$value}%"];
})->values()->toArray();
return User::where($filters)->SimplePaginate(15);

save where clause in a variable and use it in laravel query

I have saved all of my where clause in a variable and i'm trying to join that variable inside laravel query but its not working. Here is mine code
$where .= "";
$where .= '->where("product_details.title", '.$request->title.')';
$where .= '->where("product_details.id", '.$request->id.')';
$results = DB::table('product_details').'$where'.->get();
How to use variable inside query?
you can used like that by using if condition statement for dynamic query
$results = DB::table('product_details')
if($request->title) {
$results->where("product_details.title", $request->title);
}
if($request->id) {
$results->where("product_details.id", $request->id);
}
$result->get();
I am not sure why you want to do this. The following should just work.
DB::table('product_details')
->where([
'product_details.title' => $request->title
'product_details.id' => $request->id
])
->get();
Now, let's say that you may not always have a title or id from the request, you could also do this
DB::table('product_details')
->when($request->title, function ($query, $title) {
return $query->where('product_details.title', $title);
})
->when($request->id, function ($query, $id) {
return $query->where('product_details.id', $id);
})
->get();
this will not work because you are saving text in string and output will be on string you need to convert it to eval Try This.
$results = DB::table('product_details') . eval ($where) .->get();

How to do a where() on any column in model

I have four columns that I want to do a LIKE query on, user_id,client_id, and start_time, and end_time
I have the current statement
Model::where('client_id','LIKE','%'.$q.'%')
However, instead of it being client_id, I'd like to be able to search through all four of those columns rather then just one, without using a whereOr as that does not work efficiently for my problem
Thank you in advance.
You may do like this below.
Model::where(function($q) {
$q->where('user_id','LIKE','%'.$q.'%')
->orWhere('client_id','LIKE','%'.$q.'%')
->orWhere('start_time','LIKE','%'.$q.'%')
->orWhere('end_time','LIKE','%'.$q.'%');
});
Hope that helps.
You can use when and filter if you have setted one or another variable:
Model::when(isset($client_id),function($query) use ($client_id) {
$query->where('client_id','=', $client_id)
})
->when(isset($user_id),function($query) use ($user_id) {
$query->where('user_id','=', $user_id)
})
->when(isset($start_time),function($query) use ($start_time) {
$query->where('start_time','=',$start_time)
})
->get();
You can use where and Search records
$user_id = $request->user_id ? '%' . $request->user_id . '%' : '';
$client_id = $request->client_id ? '%' . $request->client_id . '%' : '';
$modal = Modal::where(function($q) use($user_id, $client_id) {
$q->where("user_id", "like", "$user_id")
->orwhere("client_id", "like", "$client_id");})->get();
dump($modal->count()); // count display
Hope that helps.

Laravel Like Eloquent with array value?

How can I do a Like-query, with multiple values to search for?
$searchWords = explode(' ', Input::get('search'));
Then I get an array of words that were given for the search.
How can I pass it in this:
$pages = Page::where('content', 'LIKE', '%'.$singleWord.'%')->distinct()->get();
A loop can't work, then it overwrites the $pages always; then it keeps always the latest search:
foreach($searchWords as $word){
$pages = Page::where('content', 'LIKE', '%'.$word.'%')->distinct()->get();
}
A loop is the solution but you only need to add the where condition without executing the query
$pages = Page::query();
foreach($searchWords as $word){
$pages->orWhere('content', 'LIKE', '%'.$word.'%');
}
$pages = $pages->distinct()->get();
i will code it for you :
$pages = Page->where(function($query) use($word){
foreach($searchWords as $word){
$query->orWhere('content', 'LIKE', '%'.$word.'%');
}
})->get();
you can try that , i hope that will help :)

Resources