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
Related
I have the following SQL query
$query
->join('cities','tickets.city_id','=','cities.id')
->select(
'tickets.id',
'tickets.biker_id',
'tickets.picked_up',
'tickets.delivered',
'tickets.service_charge',
'tickets.amount',
'tickets.cancelled',
'tickets.pre_order',
'tickets.created_by',
'tickets.created_at'
)
->whereDay('tickets.created_at',Date('d'))
->orderBy('tickets.created_at','desc')
->get();
my aim is to set the
whereday('tickets.created_at', Date('d'))
to
whereday('tickets.created_at', Date('d', strtotime("-1 day")))
whenever the tickets.pre_order = 1
but when tickets.pre_order = 0 i will stick to the
whereday('tickets.created_at',Date('d'))
is it possible using if statement or is there any better way to solve this?
make it like this
->where(function ($query){
$query->where('tickets.created_at', Carbon::now()->subDays()->format('d'))
->where('tickets.pre_order',1);
})->orWhere(function ($query){ $query->where('tickets.created_at',
Carbon::now()->format('d')) ->where('tickets.pre_order',0); })
->get();
To subtract a day and format it, use Carbon library for DateTime in PHP (as given in comments by #spartyboy )
$query
->join('cities','tickets.city_id','=','cities.id')
->select(
'tickets.id',
'tickets.biker_id',
'tickets.picked_up',
'tickets.delivered',
'tickets.service_charge',
'tickets.amount',
'tickets.cancelled',
'tickets.pre_order',
'tickets.created_by',
'tickets.created_at'
)
->where(function ($query) {
$query
->where('tickets.pre_order', 0)
->whereDay('tickets.created_at', Date('d'));
})
->orWhere(function ($query) {
$query
->where('tickets.pre_order', 1)
->whereDay('tickets.created_at', Carbon::yesterday()->format('d'));
})
->orderBy('tickets.created_at','desc')
->get();
or if you want to subtract multiple days then
instead of yesterday() use now()->subDays($days_count)
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();
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.'%');
});
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();
Hello i am building a delete query to delete an row out of my database in laravel and i came to this problem i need to find a row where ... and ... or ... and ...
So in normal php that would be
DELETE * FROM connection_requests
WHERE `user_id`=$user->id AND `selected_user_id`=$id OR
`selected_user_id`=$user->id AND `user_id`=$id;
(The first and belongs to the where and the second and belongs to the or)
now how do i do this in laravel
this would be work try its
DB::table('connection_requests')
->where(['user_id'=>$user->id, 'selected_user_id'=>$id])
->orWhere(['user_id'=>$id, 'selected_user_id'=>$user->id])
->delete();
You may try this
DB::table('connection_requests')
->where(function ($query) use ($user->id, $id) {
$query->where('user_id', '=', $user->id)
->where('selected_user_id', '=', $id);
})
->orWhere(function ($query) use ($user->id, $id) {
$query->where('selected_user_id', '=', $user->id)
->where('user_id', '=', $id);
})
->delete();
i fixed it myself everyone thanks for thinking with me
DB::table('connection_requests')
->where('user_id', $user->id)
->where('selected_user_id', $id)
->orWhere('selected_user_id', $user->id)
->orWhere('user_id', $id)
->delete();