Using orWhere in a multiple where query builder Laravel - php

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 ?
)

Related

Laravel eloquent returning wrong data

I have conditional data and it ignores that conditions while returning data:
Works fine
$orders = Order::where('user_id', $user->id)->with(['customer', 'laundry', 'driver', 'driver.user', 'progresses' => function($p){
$p->orderby('created_at', 'asc');
}, 'progresses.progress', 'services'])->get();
Return wrong data
$orders = Order::where('user_id', $user->id)->with(['customer', 'laundry', 'driver', 'driver.user', 'progresses' => function($p){
$p->orderby('created_at', 'asc');
}, 'progresses.progress', 'services'])
->where('id', 'like', '%'.$this->search.'%')
->orWhere('transport', 'like', '%'.$this->search.'%')
->orWhere('amount', 'like', '%'.$this->search.'%')
->orWhere('weight', 'like', '%'.$this->search.'%')
->orWhere('total', 'like', '%'.$this->search.'%')
->paginate(10);
Issue
The second query return all orders and ignores where('user_id', '=', $user->id).
Question
why it ignore where('user_id', '=', $user->id)
How to fix it?
You should group your search LIKE query here
Order::with(
[
'customer',
'laundry',
'driver',
'driver.user',
'progresses' => function($p) {
$p->orderby('created_at', 'asc');
},
'progresses.progress',
'services'
]
)->where('user_id', $user->id)
->where(function($q) {
$q->where('id', 'like', '%'.$this->search.'%')
->orWhere('transport', 'like', '%'.$this->search.'%')
->orWhere('amount', 'like', '%'.$this->search.'%')
->orWhere('weight', 'like', '%'.$this->search.'%')
->orWhere('total', 'like', '%'.$this->search.'%');
})->paginate(10);
So the query string is look like:
WHERE user_id = ?
AND
(id LIKE ? OR transport LIKE ? OR amount LIKE ? OR weight LIKE ? OR total LIKE ?)
It will ignore the where('user_id', '=', $user->id) because of orWhere on your search LIKE query

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

Laravel join with "and" and "or" in the where clause

How can I write a Laravel 5 Eloquent query like this SQL?
where ( (table1.fname like %xxxxx% )
OR (table1.lname like %xxxxx%) )
AND table1.is_active != 0
I have already tried
->where('users.is_active', '!=', 2)
->where('users.name', 'like' , '%'. $search .'%')
->orWhere('users.lname', 'like' , '%'. $search .'%')
->orWhere('users.email', 'like' , '%'. $search .'%') ->get();
Take a look at the Advanced join Clauses in Laravel Documentation
DB::table('users')
->join('contacts', function ($join) {
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
joins
EDIT
I think i misunderstood the question. To do nested wheres you can do the following thing:
\DB::table('table1')->...->where(function($query) {
$query->where('table1.fname', 'like', '%xxxxx$')->orWhere('table1.lname', 'like', '%xxxx%);
})->where('table1.is_active', '!=', 0);
You can use Model for this:
TableModel::where('fname', 'like', '%xxxxx%')
->orWhere('lname', 'like', '%xxxxx%')
->where('is_active', '!=', 0)
->get();
$data = DB::table('table1')->where('fname', 'like', '%xxxxx%')
->orWhere('lname', 'like', '%xxxxx%')
->where('is_active', '!=', 0)
->get();
//add use DB; in your controller
To have grouped sub-clauses that should evaluate as a whole, you can pass where() a closure. Then you want a second where call for the check on the is_active column:
$value = '%xxxxx%';
$collection = Model::where(function ($models) use ($value) {
$models->where('fname', 'like', $value)
->orWhere('lname', 'like', $value);
})
->where('is_active', '!=', 0)
->get();

Laravel Eloquent Search Multiple Related Tables

I am struggling with figuring out how to run a like query against multiple related tables.
I have a submissions table that has related users, mcd_forms, and submission_statuses tables.
Here is my code for running a LIKE statement with the given $terms_like.
$submission = new Submission;
$terms_like = '%'.$search_terms.'%';
$data['submissions'] = $submission
->join('users as users', 'users.id', '=', 'submissions.user_id')
->join('mcd_forms as forms', 'forms.submission_id', '=', 'submissions.id')
->join('submission_statuses as statuses', 'statuses.id', '=', 'submissions.submission_status_id')
->where(function($q) use ($terms_like) {
$q->where('users.user_group_id', '=', Auth::user()->user_group_id)
->orWhere('forms.name', 'like', $terms_like)
->orWhere('forms.custom_id', 'like', $terms_like)
->orWhere('forms.start_date', 'like', $terms_like)
->orWhere('forms.end_date', 'like', $terms_like)
->orWhere('forms.soft_sell_date', 'like', $terms_like)
->orWhere('forms.region', 'like', $terms_like)
->orWhere('statuses.status_formatted', 'like', $terms_like);
});
No matter what I try it returns incorrect results. What am I doing wrong?
In your query above, since you are not using the "%" symbol, your like clause is working as an "=" since it's trying to match the whole word.
Replace all the "where" clause like this:
->orWhere('forms.name', 'like', "%".$terms_like."%")
This will try to match the word anywhere in the text.
You can try 'like' operator following this:
$users = DB::table('users')
->where('forms.name','LIKE', '%'.$terms_like.'%')
->get();

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

Categories