Laravel search with multiple `where`s - php

I'm trying to do a search using where in po website.
My code:
public function globalSearch($subject) {
$users = User::where('username', 'LIKE', '%'.$subject.'%')->get();
$users = User::where('steam_id', 'LIKE', '%'.$subject.'%')->get();
$results = array();
foreach ($users as $user) {
$results[$user->username] = url('player/'.$user->slug);
}
return $results;
}
But it's only searching for steam_id and if I put username it does not find anything. I think the problem is, that I need to use multiple wheres in 1 line. Sorry if you don't understand what I mean but how can I use 1 where with multiple fields?

Below two lines are overriding your search.So remove that two lines and write it as below.
$users = User::where('username', 'LIKE', '%'.$subject.'%')->get();
$users = User::where('steam_id', 'LIKE', '%'.$subject.'%')->get();
User below code.
$users = User::where('username', 'LIKE', '%'.$subject.'%')->orwhere('steam_id', 'LIKE', '%'.$subject.'%')->get();

You're overwriting $users with that second statement. If you want a result with both where clauses, you can use an array as options:
$users = User::where([
['username', 'LIKE', '%'.$subject.'%'],
['steam_id', 'LIKE', '%'.$subject.'%']
])->get();
More on Laravel where clauses.

Will this do the job?
public function globalSearch($subject)
{
return User::where('username', 'LIKE', '%'.$subject.'%')->orWhere('steam_id', 'LIKE', '%'.$subject.'%')->get();
}
Ideally group by the table index to not get unexpected results.

You can either use an array inside of the User::where() like #kerbholz answers states correctly. Or you can go for a chaining like so:
User::where('foo', 1)->where('bar', false)->get();
Which will result in the same result as the answer above but in my opinion is usually more readable.

Related

Laravellocalization search in different language

I'm trying to search in different language.
How i read in documentation i must use ' join ' on translations but it don't working.
Every search works except translations.
Controller:
public function webSearch(Request $request)
{
$translations = \DB::table('articles')->join('translations')->where('value', 'LIKE', "%{$request->search}%")->orderBy('published_at', 'Desc')->get();
$articles = Article::where('title', 'LIKE', "%{$request->search}%")->orderBy('published_at', 'Desc')->get();
$episodes = Episode::where('title', 'LIKE', "%{$request->search}%")->orderBy('published_at', 'Desc')->get();
$quizzes = Quizze::where('title', 'LIKE', "%{$request->search}%")->orderBy('published_at', 'Desc')->get();
$shows = Show::where('title', 'LIKE', "%{$request->search}%")->orderBy('published_at', 'Desc')->get();
return view('pages/search')
->with('articles', $articles)
->with('episodes', $episodes)
->with('quizzes', $quizzes)
->with('shows', $shows)
->with('translations', $translations);
}
Join should be used Like this:
The first argument passed to the join method is the name of the table you need to join to, while the remaining arguments specify the column constraints for the join.
$translations = \DB::table('articles')
->join('translations','articles.id','=','translations.article_id')
->where('value', 'LIKE', "%{$request->search}%")->orderBy('published_at', 'Desc')->get();

Laravel how to write search query with multiple conditions but where records under the same user

I have a database with inventory products. Also, I have many users where products belonged to different users.
So I need to write a query with multiple conditions, where search needs to be in different table columns, so it will be a couple of ->orWhere() statements. But the search needs to find all the products that belonged to the same user. But if I have many ->orWhere() conditions, query starts searching the products under other users where other queries conditions match ("title" or "product_number").
So how can I change my code, where I can get all products with search keyword where the products under the same user?
Here is my code:
public function searchProduct(Request $search){
$search_item = $search->search;
$user = User::where('id', Auth::user()->id)->first();
$inventory = Inventory::where('client_id', $user->client_id)
->where('product_number', 'like', "%{$search_item}%")
->orWhere('title', 'like', "%{$search_item}%")
->orWhere('other_product_numbers', 'like', "%{$search_item}%")
->with(['deliveryRecord', 'orderInRecord', 'sellRecord'])
->paginate(50);
$cleint_currency = SettingsClientCurrency::where('client_id', $user->client_id)->first();
$inventory_products = Inventory::where('client_id', $user->client_id)->with('orderInRecord')->get();
$inventory_total = 0;
foreach ($inventory_products as $product) {
$ordersin = $product->orderInRecord;
foreach ($ordersin as $orderin) {
$inventory_total += $orderin['price'] * $orderin['units'];
}
}
return view('layouts.inventory.inventory')
->with('inventory', $inventory)
->with('inventory_total', $inventory_total)
->with('cleint_currency', $cleint_currency)
->with('user', $user);
}
you can use two where clause with a callback function in the second where clause. that will get your desired result
$inventory = Inventory::where('client_id', $user->client_id)
->where(function ($query) use ($search_item) {
$query->where('product_number', 'like', "%{$search_item}%")
->orWhere('title', 'like', "%{$search_item}%")
->orWhere('other_product_numbers', 'like', "%{$search_item}%");
})
->with(['deliveryRecord', 'orderInRecord', 'sellRecord'])
->paginate(50);

Laravel Nested Eager Loading how to use eloquent where condition from inside with nested

I'm looking for a way to use where condition in my laravel eloquent relationship with eager loading.
Here's my code
$data = Model::with(['stage_chatbot'=> function($stage_chatbot){
$stage_chatbot->select('id', 'customer_id','stage','created_at');
$stage_chatbot->orderBy('id', 'asc');
}])->whereIn('hot_leads_id', $getMaxHl)->orderBy('updated_at', 'desc');
$data->where('stage_chatbot.stage', 'like', 'sampleValue')->get();
return $data;
Thanks!
try this one
using use keyword in laravel
$searchText = "sampleValue";
$data = Model::with(['stage_chatbot'=> function($stage_chatbot) use ($searchText) {
$stage_chatbot->select('id', 'customer_id','stage','created_at')
->where('stage_chatbot.stage', 'like', "%".$searchText."%")
->orderBy('id', 'asc');
}])->whereIn('hot_leads_id', $getMaxHl)->orderBy('updated_at', 'desc')->get();
for more information how to used use keyword in anonymous function see
You can use whereHas() eloquent method like this.
$data = Model::with(['stage_chatbot'=> function($stage_chatbot){
$stage_chatbot->select('id', 'customer_id','stage','created_at');
$stage_chatbot->orderBy('id', 'asc');
}])->whereIn('hot_leads_id', $getMaxHl)->orderBy('updated_at', 'desc');
$data->whereHas('stage_chatbot', function ($stage_chatbot) {
$stage_chatbot->where('stage', 'like', 'sampleValue')
})->get();
return $data;
Define a varibale on your Search Input field or value and pass this through use on your model query
Try this
$search = $request->search;
$data = Model::with(['stage_chatbot'=> function($stage_chatbot) use ($search) {
$stage_chatbot->select('id', 'customer_id','stage','created_at');
$stage_chatbot->where('stage', 'like',$searchText);
$stage_chatbot->orderBy('id', 'asc');
}])->whereIn('hot_leads_id', $getMaxHl)->orderBy('updated_at', 'desc')->get();

How to make a multiple where clause with Laravel Elequent?

$data = Retour::where('status','=',3)->where('rma','LIKE', '%' .$searchquery.'%')->OrWhere('naam','LIKE', '%' .$searchquery.'%')->OrWhere('framenummer','LIKE', '%' .$searchquery.'%')->get();
What's wrong with this query?
It ignores the where status = 3..
and returns all the reconrds even where the status != 3..
Thanks in advance.
You should group orWhere() here with closure:
$data = Retour::where('status', 3)
->where(function($q) use($searchquery) {
$q->where('rma', 'like', '%'.$searchquery.'%')
->orWhere('naam', 'like', '%'.$searchquery.'%')
->orWhere('framenummer', 'like', '%'.$searchquery.'%');
})->get();
Use scopes in such cases. See https://laravel.com/docs/5.3/eloquent#local-scopes
public function scopeSearch($query, $searchQuery)
{
return $query->where('status',3)
->where('rma','LIKE', "%$searchQuery%")
->orWhere('naam','LIKE', "%$searchQuery%")
->orWhere('framenummer','LIKE', "%$searchQuery%");
}
and one more thing
its not OrWhere it is orWhere

Query for multiple fields with Laravel

So I am trying to create a search functionality so that you can query for both first and last name. What can I use as an AND statement for the query?
public function find()
{
$search = Input::get('contact_search');
$query = Contact::orderBy('name', 'desc');
if (is_null($search))
{
$contacts = $query->paginate(15);
//return View::make('contacts.index')->with(array('contacts' => $contacts));
} else {
$contacts = $query->where('firstName', 'LIKE', "%{$search}%")
->orWhere('lastName', 'LIKE', "%{$search}%")
->paginate(15);
$contact = Contact::find(1);
}
return View::make('hello')->with(array('contacts' => $contacts));
}
I have tried
$query->where('firstName', 'LIKE', "%{$search}%")
->Where('lastName', 'LIKE', "%{$search}%")
but that does not work either. Any advice would be awesome! Thanks.
where() acts as an and statement already. Just chain them together.
$people = People::where('first_name','=','John')->where('last_name','=','Doe')->get();
In your query, you have ->Where. Make sure its lowercase.
Also, your method could use some optimization. You are searching for multiple contacts and paginating the results, but then you are doing a find(1) for some reason. A better approach is to just do the following:
$contact = Contact::orderBy('name','desc')
->where('firstName', 'LIKE', "%{$search}%")
->where('lastName', 'LIKE', "%{$search}%")
->first();
That will return your first contact in the results. No need for pagination. And find() actually searches for records based off of id's anyways so you don't want to use that in this case.
If you use where it will add AND if you use multiple where. However I don't know if in your case you want to use AND because you if someone will put into form Jo you will search for poeple that hat Jo both in name and surname, so for example John Smith won't be found here because his surname doesn't contain Jo.
So answering your question you could use:
$contacts = $query->where('firstName', 'LIKE', "%{$search}%")
->where('lastName', 'LIKE', "%{$search}%")
->paginate(15);
but probably this won't make much sense.
It's hard to also say what exactly you do here, because you have here:
$contacts = $query->where('firstName', 'LIKE', "%{$search}%")
->orWhere('lastName', 'LIKE', "%{$search}%")
->paginate(15);
$contact = Contact::find(1);
using those 2 lines first you look for people who have $search in first or last name and paginate them, and when using Contact::find(1); you find person with id 1. It also doesn't seem to be good solution to anything here.
If you would like to find the first record that have $search either in first or last name, you should use:
$contacts = $query->where('firstName', 'LIKE', "%{$search}%")
->where('lastName', 'LIKE', "%{$search}%")
->first();
without orWhere and without paginate.

Categories