How to use two where clauses with like operator in Laravel - mysql

I am using like operator two times in different where clauses. If I try one where clause, It works fine, but with both. It doesn't work.
Here is my code.
$products = Product::where(['is_live'=>'N'])->where(['is_active'=>'Y'])->where(['is_deleted'=>'N']);
$products->where('payment_mode', 'like', '%Credit/Debit Card%');
$products->where('payment_mode', 'like', '%Cash%');
$products = $products->get();
Please help.

You must be use orWhere
$products = Product::where(['is_live'=>'N'])
->where(['is_active'=>'Y'])
->where(['is_deleted'=>'N'])
->where(function($query) {
$query->where('payment_mode', 'like', '%Credit/Debit Card%')
->orwhere('payment_mode', 'like', '%Cash%');
});
or you can use this
Product::where([
'is_live'=>'N',
'is_active'=>'Y',
'is_deleted'=>'N'
])->where(function ($query) {
$query
->where('payment_mode', 'like', '%Credit/Debit Card%')
->orwhere('payment_mode', 'like', '%Cash%');
})->get();

Related

What is the right way to write this query?

I have a query that is not returning all the results it should.
I'm using Laravel, but the result is the same if I write the query directly in PHPMyAdmin:
clientpages::selectRaw('page as value, COUNT(ID) as `count`')
->where('page', 'like', '%blog%')
->orWhere('page', 'like', '%home%')
->whereBetween('date', [$range['from'], $range['to']]);
The problem is that the query only processes the first like. It only returns the pages that have blog in them and not the ones with home.
The culprit is the date condition. If I remove that, it works as expected, but what is the right way to use these three conditions all at once?
Like you would wrap a normal SQL query in parentheses, you can do the same in Laravel like this:
clientpages::selectRaw('page as value, COUNT(ID) as `count`')
->where(function($query) {
$query->where('page', 'like', '%blog%')
->orWhere('page', 'like', '%home%')
})
->whereBetween('date', [$range['from'], $range['to']]);
$clientpages = DB::table('clientpages')
->where('page', 'like', '%blog%')
->orWhere('page', 'like', '%home%')
->whereBetween('date', [$range['from'], $range['to']])
->get(); //or count() if you just want the count
clientpages::where('page', 'like', '%blog%')
->orWhere('page', 'like', '%home%')
->whereBetween('date', [$range['from'], $range['to']])
->get(); //or count() if you just want the count

orWhere with multiple conditions in Eloquent

I am looking to write the following query in eloquent:
select * from stocks where (symbol like '%$str%' AND symbol != '$str' ) OR name like '$str%'
Without the last condition, it's simple:
$stocks = Stock::live()
->where('symbol','like','%'.$str.'%')
->where('symbol','!=',$str)
->get();
But adding orWhere('name','like',$str.'%') after the two wheres returns incorrect results. Basically I am wondering how to emulate what I accomplished by using the (condition1 AND condition2) OR condition3 syntax in the raw query above.
Try
$stocks = Stock::live()
->where('name', 'like' , '%'.$str.'%')
->orWhere(function($query) use($str) {
$query->where('symbol','like','%'.$str.'%')
->where('symbol','!=',$str); // corrected syntax
})->get();
Try
$stocks = Stock::live()->where('name', 'like' , '%'.$str.'%')
->orWhere(function($query) use($str) {
$query->where('symbol','like','%'.$str.'%')
->where('symbol','!=',$str)
})->get();
I didn't test this, so sorry if it doesn't work. But I think one of these solutions could work.
$stocks = Stock::live()
->where([
['symbol','like','%'.$str.'%'],
['symbol', '!=', $str],
])
->orWhere('name','like', $str.'%')
->get();
and
->where(function ($query) use ($str) {
$query->where([
['symbol','like','%'.$str.'%'],
['symbol', '!=', $str],
]);
})
->orWhere(function ($query) use ($str) {
$query->where('name','like', $str.'%');
});

How to use OR inside WHERE in laravel Query

I'm new to laravel and I need help for query in laravel
My Custom Query
$sql1="SELECT * FROM blogs
WHERE
('$title'='' OR title='$title')
AND
('$body'='' OR body='$body')";
and i create a laravel build query but don't know how to put OR inside WHERE and Put Brackets
$posts = Blog::where('title','LIKE',"%{$title}%")
->Where('body', 'LIKE',"%{$body}%")
->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
I think orWhere is what you're looking for and based off custom query I think you would need = instead of like unless thats what youre going for, I think it would be something like:
->where('title', '=', $title)
->orWhere('title', '=', '');
$posts = Blog::where(function($q) use($title){
$q->where('title','')->orWhere('title',$title);
})->where(function($q) use($body){
$q->where('body','')->orWhere('body',$body);
})->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
Just use orWhere()
$posts = Blog::where('title', $title)
->orWhere('title', $otherTitle)
->orderBy($order, $dir)
->get();
Your query should be like :
$result = Blog::where(function($query) use ($title, $body){
$query->where(
["title" => $title, "body"=>$body]
)->orWhere(
["title"=>"", "body"=>""]
);
})
->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
Hope this give you an idea :)
$posts = Blog::where(function ($query) use($title) {
$query->where('title', 'LIKE', "%{$title}%")
->orWhere('title', '=', '$title');
})
->orWhere(function ($query) use($body) {
$query->where('body', 'LIKE', "%{$body}%")
->orWhere('body', '=', '$body');
})
->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
Not tested...
Use the ->orWhere() code.
Note: You can also make use of magic methods
->whereAgeOrPhone(18, 123456789);
use orWhere()
$blogs= DB::table('Blog')
->where('title', 'LIKE', '%{$title}%')
->orWhere('body','LIKE', '%{$body}%')
->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
For More Query, You can visit here

Laravel 5.2 WHERE LIKE has either a question mark or string without quotes

I'm trying to complete a search form in Laravel 5.2. I've run into a problem where the $search will be a question mark in my sql query. I've used ->toSql() to get the result of the query. I'm using DB::table() with a WHERE like this:
->where('clientname', 'LIKE', $search)
->orWhere('ClientFirstName', 'LIKE', [$search])
->orWhere('ClientLastName', 'LIKE', DB::raw($search))
Each of these create in different results in the query:
"where clientname LIKE ? or ClientFirstName LIKE ? or ClientLastName LIKE test "
Using the DB::raw() I get the search parameter from the form, but I can't make it a wildcard search with %, and I can't put single or double quotes around it to have the proper syntax to complete the search query.
Any advice is appreciated!
Try using this code
->where('clientname', 'LIKE', '%'.$search.'%')
->orWhere('ClientFirstName', 'LIKE', '%'.$search.'%')
->orWhere('ClientLastName', 'LIKE', '%'.$search.'%')->get();
->where(function ($q) use ($searchValues) {
foreach ($searchValues as $value) {
$q->orWhere('ClientFirstName', 'LIKE', "%{$value}%");
}
})
->orWhere(function ($q) use ($searchValues) {
foreach ($searchValues as $value) {
$q->orWhere('ClientLastName', 'LIKE', "%{$value}%");
}
})
I ended up going with this as my final working solution.

Check overlaping timeslots for laravel query

While booking a time-slot I want to check if that slot is overlapping with any other already booked time-slot. Used following query for same which is not correct:
Availability::where('date',date('Y-m-d',strtotime($request->available_date)))
->where(function ($query) use($request){
$query->whereBetween('start_time', [date("H:i:s", strtotime($request->start_time)), date("H:i:s", strtotime($request->end_time))])
->orWhereBetween('end_time', [date("H:i:s", strtotime($request->start_time)), date("H:i:s", strtotime($request->end_time))]);
})
->get();
Thanks In advance!
Firstly, to be able to have access to $startTime and $endTime within the query closure you will need to pass them through using the use construct i.e.
function ($query) use ($startTime, $endTime)
Try like below:
$startTime = $request->start_time;
$endTime = $request->end_time;
$Availability = Availability::where(function ($query) use ($startTime, $endTime) {
$query
->where(function ($query) use ($startTime, $endTime) {
$query
->where('start_time', '<=', $startTime)
->where('end_time', '>', $startTime);
})
->orWhere(function ($query) use ($startTime, $endTime) {
$query
->where('start_time', '<', $endTime)
->where('end_time', '>=', $endTime);
});
})->count();
Hope this will helps you!
Little Modified Query Hope it will work for all the people searching time slot booking algorithm.
$Availability= Availability::
where(function ($query) use ($start_time, $end_time) {
$query
->whereBetween('start_time', [$start_time, $end_time])
->orWhere(function ($query) use ($start_time, $end_time) {
$query
->whereBetween('end_time', [$start_time, $end_time]);
});
})->count();