Combining AND/OR eloquent query in Laravel - php

How can I write following or similar kind of queries using Eloquent?
SELECT * FROM a_table WHERE (a LIKE %keyword% OR b LIKE %keyword%) AND c = 1 AND d = 5
I couldn't combine AND/OR in the way I wanted by chaining where & or_where functions.

You can nest where clauses : http://laravel.com/docs/database/fluent#nested-where
Model::where(function($query)
{
$query->where('a', 'like', 'keyword');
$query->or_where('b', 'like', 'keyword');
})
->where('c', '=', '1');
This should produce : SELECT * FROM models WHERE (a LIKE %keyword% OR b LIKE %keyword%) AND c = 1

For a more accurate answer to the example:
$val = '%keyword%';
A_Table_Model::where(function($query) use ($val)
{
$query->where('a', 'like', $val);
$query->or_where('b', 'like', $val);
})
->where('c', '=', 1)
->where('d', '=', 5)
->get();
Note: This is Laravel 3 syntax, use camelCase orWhere() for Laravel 4

In Laravel 5.1+ this will also do the job and looks cleaner:
Model::where(function($query) {
$query->where('a', 'like', 'keyword');
$query->or_where('b', 'like', 'keyword');
})->where('c', '=', '1')->get();

You can use DB::raw() in the first where() to put in the like/or statement.

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();

Using orWhere in a multiple where query builder Laravel

I want to select from database pads with 4 conditions, with 1 condition have to use "or". I wrote this query builder, it works. But I want to know if i can use a shorter query. Like use 3 conditions in a "where" brackets, and 1 in "orWhere" bracket. This is my query:
$pads = DB::table('pads')
->join('languages', 'pads.language_id', '=', 'languages.id')
->select('pads.id', 'pads.title', 'pads.status', 'pads.people', 'pads.created_at', 'languages.name as language')
->where([
['pads.user_id', Auth::user()->id],
['pads.title', 'LIKE', "%{$request->search}%"],
['pads.status', $request->status],
['pads.language_id', $request->lg_id]
])
->orWhere([
['pads.user_id', Auth::user()->id],
['pads.people', 'LIKE', "%{$request->search}%"],
['pads.status', $request->status],
['pads.language_id', $request->lg_id]
])
->orderBy('created_at', 'desc')
->get();
You have to group your conditions within the OR within a orWhere() with a function as the argument.
->where([
['pads.user_id', Auth::user()->id],
['pads.status', $request->status],
['pads.language_id', $request->lg_id]
])
->where(function ($query) use ($request) {
$query->where('pads.title', 'LIKE', "%{$request->search}%")
->orWhere('pads.people', 'LIKE', "%{$request->search}%");
})
This will produce a query similar to
WHERE pads.user_id = ?
AND pads.status = ?
AND pads.language_id = ?
AND (
pads.title LIKE ?
OR pads.people LIKE ?
)

Laravel where clause is not returning data after multiple orWhere clauses?

I am using laravel 6.10 version in that I am implementing search,
I have 3 tables
1) course_category
2) course_sub_category
3) course [course_id_category(foreign key),course_sub_category_id(foreign key)]
below is my code
$course = $request->searchItem;
if ($course=="") {
$Courses = DB::table('course')
->join('course_category', 'course.course_category_id', '=', 'course_category.id')
->select('course.*','course_category.title as category_title','course_category.thumb as category_thumb')
->orderBy('course.title','asc')
->paginate(15);
}
else{
$Courses = DB::table('course')
->join('course_category', 'course.course_category_id', '=', 'course_category.id')
->join('course_sub_category', 'course.course_sub_category_id', '=', 'course_sub_category.id')
->select('course.*','course_category.title as category_title','course_category.thumb as category_thumb')
->where('course.title', 'LIKE', '%'.$course.'%')
->orWhere('course_category.title', 'LIKE', '%'.$course.'%')
->orWhere('course_sub_category.title', 'LIKE', '%'.$course.'%')
->get();
}
when i am retunring the values i am gatting 0 arrays but when i am removing one orwhere from existing my query is working and its returnig we all values with match
means when i am using multiple orWhere in laravel my where is not working, please share the solution on same.
->orWhere doesnt work like typical SQL. You should use it like that:
$Courses = DB::table('course')
->join('course_category', 'course.course_category_id', '=', 'course_category.id')
->join('course_sub_category', 'course.course_sub_category_id', '=', 'course_sub_category.id')
->select('course.*','course_category.title as category_title','course_category.thumb as category_thumb')
->where(function($query) use ($course){
$query->where('course.title', 'LIKE', '%'.$course.'%')
$query->orWhere('course_category.title', 'LIKE', '%'.$course.'%')
$query->orWhere('course_sub_category.title', 'LIKE', '%'.$course.'%')
})
->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

how to handle Multiple Orwhere in Laravel

I'm working in Laravel 5 and am using the Orwhere clausule, my code to consult is this:
$estudiantes= Usuario::
where('rol', '=', 'ESTUDIANTE')
->whereHas('perfil', function($query) use ($data){
if($data!="")
$query->Where('doc_identidad', '=' ,$data)
->orWhere('nombres','like','%'.$data.'%')
->orWhere('apellidos','like','%'.$data.'%');
dd($query);
})->get();
So, I want to filter every Usuario with rol ESTUDIANTE that works very fine, then I want to filter which of these have doc_identidad or nombres or apellidos like you see, I have a dd($query) to see the tree of the query, and I noticed that the first where uses and instead of or. This is the tree:
So, I have the next:
where1 and where2 or where3 or where4
and I want:
where1 and (where2 or where3 or where4)
so, how should I do it? thanks!
where1(a=1) and (where2(b=1) or where3(c=1) or where4(d=1))
Model::where(function ($query) {
$query->where('a', '=', 1);
})->where(function ($query) {
$query->where('b', '=', 1)
->orWhere('c', '=', 1)
->orWhere('d', '=', 1);
});

Categories