Laravel "where" clause with "like" comparator - php

I have an array:
$box = implode(',', $ch);
dd($box)
That returns:
"windows,linux,ios,android"
(array can contain 1, 2, 3 or 4 elements like that)
I have this eloquent query:
$vpns = Vpns::where('systemes', 'like', '%'.$box.'%')->get();
My problem is that only works if the array contains only one element. If it contains more, it returns nothing.

I think no need to use implode .wherein can be better option if you searching exact word.
$vpns = Vpns::whereIn('systemes',$ch)->get();
if you still looking for like in query then
$vpns = Vpns::where(function($query)use($ch){
foreach($ch as $value){
$query->where('systemes', 'like', '%'.$value.'%');
}
})->get();
Updated
Vpns::where(function($query)use($ch){
foreach($ch as $value){
$query->orWhere('systemes', 'like', '%'.$value.'%');
}
})->orWhere('systemes', 'like', '%'.$box.'%')
->where('netflix', '=', $netflix)
->where('torrent', '=', $torrent)
->where('nolog', '=', $nolog)
->where('chine', '=', $chine)
->orderBy('prix')->get();

you may need to put your conditions in an or logical connection
$vpns = Vpns::where(function($q) use ($ch) {
foreach ($ch as $system) {
$q->orWhere('systemes', 'like', '%' . $system . '%');
}
})->get();

Related

Joining two models in Laravel Eloquent

I have a model stored in a variable like:
$user = User::where('name', 'like', $test)
->orderBy('name');
I would like to build another query where I can join $user. Can't find the right syntax to do it.
What am trying to achieve:
$numbers= Number::join($user, $users.id, '=', 'number.user_id')
->where('name', 'like', "%" . $validated['text'] . "%")])
->get();
Assuming you have typical hasMany/belongsTo relationships set up between User and Number models, this should work:
User::where("name", "like", $test)
->whereHas("numbers", function($q) {
$q->where("name", "like", "%$validated[text]%");
})
->with("numbers", function($q) {
$q->where("name", "like", "%$validated[text]%");
})
->get();
The where() method, of course, matches users with the desired name. The whereHas() method further restricts based on the relationship, looking only for users having numbers with a matching name. Assuming you want to retrieve those matching numbers, you have to do the same filter again on the eager load.
Try this,
$numbers= Number::whereHas('<Your Eloquent Model>',function($query)use($validated){
$query->where('name', 'like', "%" . $validated['text'] . "%")]);
}
->get();
Join can be written in this way
$records = User::select('users.*')
->where('users.name', 'like', $test)
->join('numbers', function($join) {
$join->on('users.id', '=', 'numbers.id')
->where('numbers.name', 'like', "%" . $validated['text'] . "%");
})
->get();

Laravel Eloquent - Conditional Data Fetching

I've a customer and customer group table. I want to search the customers based on term/filter text.
Say, there is two customer_group_id, 7 and 8. When 8, I'll need to find mobile fields in orWhere clause, otherwise not.
What I've tried is
$contacts = Contact::where(function ($query) use ($term) {
$query->where('contacts.name', 'like', '%' . $term .'%')
});
// For customer_group_id=8
$contacts->when('customer_group_id=8', function($q) use ($term){
return $q->orWhere('mobile', 'like', '%' . $term .'%');
});
The when() is not working. It showing all the results. I know that I've to pass any boolean value in the when() functions first parameter.
Is there any solution for this problem? Or what is the other way I can get the data's.
The when() method doesn't add an if statement to your query, it is just a way to save you from writing an if statement in your code.
To achieve what you're after you can use nested orWhere() clause:
$contacts = Contact::where('name', 'like', '%' . $term . '%')
->orWhere(function ($query) use($term) {
$query->where('customer_group_id', 8)->where('name', 'like', '%' . $term . '%');
})
->get();
If there is more to your query than what you've put in your question then you can simply wrap the above in another where clause:
$contacts = Contact::where(function ($query) use ($term) {
$query->where('name', 'like', '%' . $term . '%')
->orWhere(function ($query) use ($term) {
$query->where('customer_group_id', 8)->where('name', 'like', '%' . $term . '%');
});
})
->where('some column', 'some value')
->get();

How to perform in laravel multiple where with or query

Product::where('name', 'like', 'something%')->orWhere('category', 'like', 'something%')->orWhere('price', 'like', 'something%')->orWhere('qty', 'like', 'something%')...
Can I do it in more elegant and compact way?
You can go about it this way. passing arrays to a single orWhere clause
Product::where('name', 'like', 'something%')->orWhere(['category', 'like', 'something%'], ['price', 'like', 'something%'],['qty', 'like', 'something%'])...
You can use the searchable, take a look here and here also here
You can use 'array_map()' to build an array for orWhere():
$search = 'something';
Product::orWhere(array_map(
function ($i) use($search) { return [$i, 'like', $search.'%']; },
['name', 'category', 'price', 'qty']
))->get();
It's a good way if you have many fields to search by. For 3-4 fields, I'd just use simple orWhere() chaining, because it's easier to read the code

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