$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
Related
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();
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();
I'm using SoftDeletesin my projects, which is recognized as deleted_at in database table, I want to search and find only deleted rows.
Here is my controller code
public function trashedJobSearch(Request $request)
{
$search = $request->get('search');
$jobs = Jobs::select('id', 'company_name', 'position_name', 'job_description','deleted_at', 'created_at', 'expire_date', 'status')
->where(DB::raw('lower(company_name)'), 'like', '%' . mb_strtolower($search) . '%')
->orWhere(DB::raw('lower(position_name)'), 'like', '%' . mb_strtolower($search) . '%')
->where('deleted_at', '!=', null)
->paginate(10);
return view('/trashed', compact('jobs'));
}
I tried to use onlyTrashed() but it's not working either.
As you have orWhere you need to use a grouping and also onlyTrashed
Jobs::select('id', 'company_name', 'position_name', 'job_description','deleted_at', 'created_at', 'expire_date', 'status')
->where(function ($query) use ($search) {
$query->where(DB::raw('lower(company_name)'), 'like', '%' . mb_strtolower($search) . '%')
->orWhere(DB::raw('lower(position_name)'), 'like', '%' . mb_strtolower($search) . '%');
})->onlyTrashed()
->paginate(10);
The onlyTrashed() method should work for you. Docs for Laravel deleted entries. I have seen times where adding the raw select statement screws it up though.
Take out the extra bits with the select and DB::raw and then add them in after you have what you need. Start with the simplest:
$testOfDeletedOnlyJobs = Jobs::onlyTrashed()->get();
From here, add in the other parts of your query to see where and why it fails. If the above gives you nothing, perhaps there are no deleted records?
You can use
Model::onlyTrashed()->get();
Did you have looking for this post yet?
How to get all rows (soft deleted too) from a table in Laravel?
You can try this.
In your Model-
use Illuminate\Database\Eloquent\SoftDeletes;
class ModelName extends Model
{
use SoftDeletes;
}
To get only deleted rows
$search = $request->get('search');
$data = App\ModelName::onlyTrashed()
->where('id', $search)
->get();
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.
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.