how to handle Multiple Orwhere in Laravel - php

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

Related

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 use laravel where clause?

I am new in Laravel.
How to convert the following query to Laravel where clause.
where category='1' and (shipping_from='1' OR shipping_to='2')
You should use nested parameter groupings: https://laravel.com/docs/5.6/queries#parameter-grouping. Eg:
->where('category','1')
->where(function ($query) {
$query->where('shipping_from', '1')
->orWhere('shipping_to', '2');
})
->where('category', 1)
->where(function ($query){
$query->where('shipping_from', 1)
->orWhere('shipping_to', 2);
})
Refer to: Parameter Grouping
Please check the documentation of parameter-grouping in Laravel 5.7
$products = Product::where('category', 1)
->where(function ($query){
$query->where('shipping_from', 1)
->orWhere('shipping_to', 2);
})->get();
Initially laravel thinks AND condition in all where conditions. until unless you specify or.
You can also try like this:-
use App\Model;
$data = Model::where('category','1')
->where(function ($query) {
$query->where('shipping_from', '1')
->orWhere('shipping_to', '2');
})->get();

Inner query with use variable

I am new to laravel and I am trying to retrieve data from three tables because the user enters 3 information then I use its to determine the row to check login
so I use this way but not work!
I do not know how to send variables in SQL
DB::table('members')
->join(DB::raw('(SELECT FROM members_courses_assign WHERE referenceNumber=>$coursenum,termkey=>$semester) courseA'), function($join) {
$join->on('members.externalPersonKey', '=', 'courseA.externalPersonKey');
})->join(DB::raw('(SELECT courses FROM WHERE referenceNumber=>$coursenum,termkey=>$semester) coursecc '), function($join) {
$join->on('courseA.referenceNumber', '=', 'coursecc.referenceNumber');
})
->where(['jobID' => $jobid])->get();
Try this :
->where('jobID',$jobid)->get();
Here in Laravel Documentation you can find all you need to know about query builder.
To pass variable to inner query (Anonymous functions) you have to pass it with use keyword as described in docs
$yourVariable= "";
DB::table('members')
->join(DB::raw('(SELECT FROM members_courses_assign WHERE referenceNumber=>$coursenum,termkey=>$semester) courseA'), function($join) use($yourVariable) {
$join->on('members.externalPersonKey', '=', 'courseA.externalPersonKey');
})->join(DB::raw('(SELECT courses FROM WHERE referenceNumber=>$coursenum,termkey=>$semester) coursecc '), function($join) use($yourVariable) {
$join->on('courseA.referenceNumber', '=', 'coursecc.referenceNumber');
})
->where(['jobID' => $jobid])->get();
DB::table('members')
->join('members_courses_assign', function($join) use ($coursenum, $semester) {
$join->on('members.externalPersonKey', '=', 'members_courses_assign.externalPersonKey')
->where('referenceNumber', $coursenum)
->where('termkey', $semester);
})
->join('courses', function($join) use ($coursenum, $semester) {
$join->on('members_courses_assign.referenceNumber', '=', 'courses.referenceNumber')
->where('referenceNumber', $coursenum)
->where('termkey', $semester);
})
->where('jobID', $jobid)
->get();
You can refer to Query Builder's advanced join clauses here.

Laravel eloquent SQL query with OR and AND with where clause

I know that sql AND can be achieved by consecutive where clauses. and OR with where(condition,'OR').
I want to choose all the messages from message table where sentTo = editor_id AND sentFrom= currentUser_id OR where sentTo = currentUser_id AND sentFrom = editor_id.
I have this query which I am trying to get the result through,
$this->data['messages'] = Message::where(function ($query) use($uId) {
$query->where('sentTo', '=', $this->data['currentUser']->id)
->orWhere('sentFrom', '=', $uId);
})
->where(function ($query) use($uId){
$query->where('sentTo', '=', $uId)
->orWhere('sentFrom', '=', $this->data['currentUser']->id);
})
->get();
How can I do this? Please advice.
I'm a bit confuse as you didn't provide parentheses to know which operator precedence you want.
Assuming you want WHERE (editor_id=$editorId AND sentFrom=$currentUserId) OR (sentTo=$currentUserId AND sentFrom=$editorId) then latest version of Laravel allows you to do:
Message::where(['editor_id' => $editorId, 'sentFrom' => $currentUserId])
->orWhere(['sentTo' => $currentUserId, 'sentFrom' => $editorId])->get();
No need to use closures.
Try this
$this->data['messages'] = Message::where(function ($query) use($uId) {
$query->where('sentTo', '=', $this->data['currentUser']->id)
->where('sentFrom', '=', $uId);
})-> orWhere(function ($query) use($uId){
$query->where('sentTo', '=', $uId)
->Where('sentFrom', '=', $this->data['currentUser']->id);
})->get();

Combining AND/OR eloquent query in Laravel

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.

Categories