sql delete query with where, and, or laravel - mysql

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

Related

how to make if statement in the laravel query builder

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)

calculating with values from two different mysql tables

I'm building a sort of human resources application where I have to reduce a variable of a mysql table with a variable of another mysql table.
The CMV I'm using is Laravel so I'm trying to use the DB class as much as possible.
This is the first table, users:
users_table
And the second table, aanvraags:
aanvraags_table
I would like reduce the variable vakantie from the first table with the variable duur from the second table. This would happen right after the vacation request is accepted. There is already a function in my controller to change the variable goedgekeurd from 0 to 1. The function looks like this:
public function updateNieuw($id){
//update aanvraag
$update_aanvraag = DB::table('aanvraags')
->where('id', $id)
->update([
'goedgekeurd' => 1 //approved by the boss
]);
Within that same function I would like to reduce the variable vakantie with the variable duur.
At the moment I already tried a few things like:
$update_vakantiedag = DB::table('aanvraags')
->select('duur')
->where('id', $id)
->get();
$vakantie = DB::table('users')
->join('aanvraags', 'users.id', '=', 'aanvraags.user_id')
->where('aanvraags.id', '=', $id)
->update([
'users.vakantie' => $update_vakantiedag
]);
All of this doesn't get me anywhere. Most of the time I get errors or the value from the variable vakantie from the first table.
It is quite important for the application that the reduction takes place. The variable vakantie displays the remaining vacation days per user after accepting a new request and is displayed in the app.
Any tips or solutions are welcome !
Thank you all in advance !
Use this:
$update_vakantiedag = DB::table('aanvraags')
->select('duur')
->where('id', $id)
->first();
$vakantie = DB::table('users')
->join('aanvraags', 'users.id', '=', 'aanvraags.user_id')
->where('aanvraags.id', '=', $id)
->decrement('users.vakantie', $update_vakantiedag->duur);
#JonasStaudenmeir Thanks a lot ! I changed the function and it works !
$update_vakantiedag = DB::table('aanvraags')
->select('duur')
->where('id', $id)
->first();
$verlof = DB::table('users')
->join('aanvraags', 'users.id', '=', 'aanvraags.user_id')
->where('aanvraags.id', '=', $id)
->first();`
$vakantie = DB::table('users')
->join('aanvraags', 'users.id', '=', 'aanvraags.user_id')
->where('aanvraags.id', '=', $id)
->update([
'users.vakantie' => $verlof->vakantie - $update_vakantiedag->duur
]);
Thank you so much !

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

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

InvalidArgumentException in JoinClause.php line 79: Not enough arguments for the on clause

I did query builder with laravel and I've used multiple join in it. When execute the query I get the error Not enough arguments for the on clause.
My query builder:
return DB::table('towns_cat_rel')
->join('towns', function ($join) {
$join->on('towns_cat_rel.town_id', '=', 'towns.id')
->where('publish', '=', 1);
})
->join('towns_translations', function ($join) {
$join->on('towns_cat_rel.town_id', '=', 'towns_translations.town_id')
->where('locale', '=', \App::getLocale());
})
->join('towns_cat', function ($join) use($categoryID) {
$join->on('towns_cat.id', '=', 'towns_cat_rel.town_cat_id')
->where('towns_cat.id', '=', $categoryID);
})
->join('towns_sub_cat_rel', 'towns_cat_rel.town_id', '=', 'towns_sub_cat_rel.town_id')
->join('towns_sub_cat', function ($join) use($subCategoryID) {
$join->on('towns_sub_cat_rel.town_sub_cat_id', 'towns_sub_cat.id')
->where('towns_sub_cat.id', '=', $subCategoryID);
})
->get();
Does anyone know why this happen?
Sorry Guys! I had forget '=' in the last join => $join->on('towns_sub_cat_rel.town_sub_cat_id', '=', 'towns_sub_cat.id')
Now it works fine!
I am using laravel5.2 and facing this above error message due to this above code and i resolved with this code, you can modify as per your requirement.
DB::table('users')
->join('contacts', function ($join) {
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);
})
->get();