I am Trying to subtract 1 minute from duration column through update query but it is not working. Duration field is of type time.
I try to use minus sign with new value but it is not working.
public function index()
{
$current = Carbon::now('Asia/Karachi');
Sale::where('date','=',$current->toDateString())
->where('time','<=',$current->toTimeString())
->update(['duration' => '- 00:01:00']);
}
I want to subtract one minute with help of this query.
Can you use DATE_SUB?
Something like
$object->update(['duration' => DB::raw('DATE_SUB(duration, INTERVAL 1 MINUTE)')])
I don't have a laravel instance in front of my, but that's how I'd write the SQL query and I think that's right based off my laravel memory.
Related
So I have a table to log profile followers every 10 minutes to keep a record of the increase/decrease.
However after the current day I only need to keep the last record of that day. Is there a simple way in Laravel to delete all records a part from the last one recorded every day.
I've tried searching and searching but comes up with nothing and feel like I'm going to create something overly complicated to accomplish this.
You'd need a query like this.
DELETE
FROM logs
WHERE id IN (
SELECT id
FROM logs
WHERE created_at BETWEEN 2022-02-28 00:00:00 AND 2022-02-28 23:59:59
ORDER BY created_at DESC
OFFSET 1
)
Assuming you have a date variable, you could make the query like this using the whereDate method:
$date = '2022-02-28'; // Y-m-d format. This is important.
DB::table('logs')
->whereIn('id', function ($sub) use ($date) {
$sub->select('id')
->from('logs')
->whereDate('created_at', $date)
->orderByDesc('created_at')
->offset(1);
})
->delete();
Greeting.
I have One datetime in my table. Its name is start_time. And I have a variable(process_time) that contains the number of seconds to run a process. How to count the number of fields with the following condition:
start_time + process_timen < Present time
You can utilize DB::raw() helper and add the SQL equivelant of what you want to achieve using DATE_ADD and INTERVAL.
$processTime = 15; // seconds
Model::where(DB::raw("DATE_ADD(start_time, INTERVAL $processTime second)"), '<', now())->count();
I have a table that stores statistics every 3 minutes with a cron job.
I want to display a chart with this data but I want the chart to have an interval of 1 hour otherwise it looks ugly as hell and is too much resource demanding.
The table has the created_at and updated_at columns.
How can I do this with eloquent?
EDIT: I want to query the records from the last 24 hours but that gives me around 480 records which is too much for a chart. I'd like to have only 24 records instead (one for every hour).
Thanks for your help!
Thanks Tim!
For anyone reading through this later, here is the solution: https://laracasts.com/discuss/channels/laravel/count-rows-grouped-by-hours-of-the-day
Model::where('created_at', '>=', Carbon::now()->subDay())->get()->groupBy(function($date) {
return Carbon::parse($date->created_at)->format('h');
});
This will allow you to get data 1 hours ago base on current time.
//Get all data for the day
$all_data = Model::where('created_at','>=',Carbon::today()->get());
//Recursive to groupBy hours
$i=1;
while ($all_data->last() != null)
{
$hourly_data = Model::where('created_at','>=',Carbon::today()->addHours($i))->get();
$all_data= $all_data->merge($hourly_data);
$i++
}
return $all_data;
I have several records in my database, the table has a column named "weekday" where I store a weekday like "mon" or "fri". Now from the frontend when a user does search the parameters posted to the server are startday and endDay.
Now I would like to retrieve all records between startDay and endDay. We can assume startDay is "mon" and endDay is "sun". I do not currently know how to do this.
Create another table with the names of the days and their corresponding number. Then you'd just need to join up your current table with the days table by name, and then use the numbers in that table to do your queries.
Not exactly practical, but it is possible to convert sun,mon,tue to numbers using MySQL.
Setup a static year and week number like 201610 for the 10th week of this year, then use a combination of DATE_FORMAT with STR_TO_DATE:
DATE_FORMAT(STR_TO_DATE('201610 mon', '%X%V %a'), '%w')
DATE_FORMAT(STR_TO_DATE('201610 sun', '%X%V %a'), '%w')
DATE_FORMAT(STR_TO_DATE('201610 tue', '%X%V %a'), '%w')
These 3 statements will evaluate to 0,1,2 respectively.
The main thing this is doing is converting the %a format (Sun-Sat) to the %w format (0-6)
well i don't know the architecture of your application as i think storing and querying a week day string is not appropriate, but i can tell you a work around this.
make a helper function which return you an array of weekdays in the range i-e
function getWeekDaysArray($startWeekDay, $endWeekDay) {
returns $daysArray['mon','tue','wed'];
}
$daysRangeArray = getWeekDaysArray('mon', 'wed');
now with this array you can query in table
DB::table('TableName')->whereIn('week_day', $daysRangeArray)->get();
Hope this help
I have days column in my users table, I need to run a cron job command using Laravel to decrease 1 from the users days column.
I know that I can use it to update all rows at once:
DB::table('Users')->update(['column' => 'value']);
But how I can set value to each member days - 1 ?
Use
DB::table('Users')->decrement('days', 1);
Or,
Since you want to decrement by 1, you can skip value part.
DB::table('Users')->decrement('days');
Find details here https://laravel.com/docs/5.3/queries#increment-and-decrement
You can update by: DB::table('Users')->update(['column' => DB::raw('value + 7')]);
Of course if you don't want to decrement or increment!