In my Laravel 9 project I have a model called UptimeCheck which contains several columns, of which I'm grabbing the following in my query:
event
response_time
I need to present a sum of all down events, up events and an average response time for the grouped check_date period.
Right now, when I even try to do a groupBy, Laravel's strict mode throws an error which prevents me from performing the query, if I remove all of my where clauses then the error goes away and the grouping works, but I need these for my query.
Here's my current starting attempt:
UptimeCheck::where('user_id', 1)
->where('monitor_id', 1)
->where('checked_at', '>=', '2023-01-01 00:00:00')
->where('checked_at', '<=', '2023-01-31 23:59:59')
->orderBy('checked_at', 'asc')
->select('event', 'response_time', DB::raw('DATE(checked_at) as check_date'))
->groupBy('check_date')
->get();
For example, this is what my query data might look like
event
response_time
check_date
up
100
2023-01-31
up
200
2023-01-31
down
400
2023-01-31
up
100
2023-01-30
What is my query missing for this behaviour to work?
Related
I've got a Laravel 8 application whereby I need to count the number of total applications for each day and output a count of the data in the most efficient way, for instance, 500 applications on 1st, 1,000 applications on 2nd and so fourth, depending on how many applications there are each day.
I'm using DB::raw() to count my rows, however, Laravel's timestamps by default includes the hours, minutes and seconds as part of the date, and so this isn't going to work for me, I need to format the date to exclude this and just contain the day.
My query doesn't seem to be returning anything, and no error either, so I think there's something wrong with my first DB::raw(), it doesn't seem to be formatted the created_at column at all, what am I missing?
Application::whereBetween('created_at', [$from, $to])
->select(DB::raw('DATE_FORMAT("created_at, %Y-%m-%d")'), DB::raw('count(*) as applications'))
->groupBy('created_at')
->get();
You can use groupByRaw()
Application::whereBetween('created_at', [$from, $to])
->select(DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d")'), DB::raw('count(*) as applications'))
->groupByRaw('DATE_FORMAT(created_at, "%Y-%m-%d")')
->get();
Also, you can use selectRaw() instead of select(DB::raw)
This is some sorting:
Get 20 posts sorted by "views".
AFTER IT FINISH, then go to the next step.
sort it by "created_at".
How to do it?
Heres my current code (which work, but I dont want to use SORTBY) for some reason sortby is not working for other project. if possible i want it to be as one eloquent query:
$this->data['today_post'] = Posts::orderBy('views', 'desc')->whereRaw('created_at >= NOW() - INTERVAL 1 DAY')->limit(20)->get();
$this->data['today_post'] = $this->data['today_post']->sortByDesc('created_at');
This code is not working, because LIMIT is usually applied as the last operation, so the result will first be sorted by "views" and "created_at" and then limited to 20. I dont want that. I want to sort and limit then after all is complete. I want to sort again the last time.
$this->data['today_post'] = Posts::orderBy('views', 'desc')->orderBy('created_at', 'desc')->whereRaw('created_at >= NOW() - INTERVAL 1 DAY')->limit(20)->get();
Thank you so much
You'll still need to use sortBy() or sortByDesc() if you want to use Eloquent. I've just tested this solution and it works perfectly:
Posts::orderBy('views', 'desc')
->where('created_at', '>=', Carbon::now()->subDay())
->take(20)
->get()
->sortByDesc('created_at');
Please help guys. I have my database:
Moreless...
I want to build a query that will count each column with a specific date for a specific page. For example Page 1 have visits 25th Dec and 26th Dec, but I want count every visit on 26th.
Somebody could help me even with pure MySQL?
This query will give you how many visitors does a specific page has on specific date. I am not sure if this is what you aim for but that's what I understood from the question.
SELECT DATE(created_at) AS date_visited, page_id, COUNT(1) AS visits
FROM your_table_name
GROUP BY DATE(created_at), page_id
As explanation - it is a basic GROUP BY usage, you can refer to the MySQL documentation http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html if you are not familiar with these. It has nothing to do with Laravel tho.
You can do it using whereDate and groupBy as:
Model::select('*', DB::raw('count(*) as count'))
->whereDate('created_at', '=', '2016-12-26')
->groupBy('page_id')
->get();
Or you can also use query builder as:
DB::table('table_name')
->select('*', DB::raw('count(*) as count'))
->whereDate('created_at', '=', '2016-12-26')
->groupBy('page_id')
->get();
I have a site that has few request options (for example add friend). I want to limit of requests made by use per certain time, for example so he can't send more than 5 friend requests within hour.
QUESTION How Can I achieve this?
I have written following query
$result = DB::table('bubble_users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '=', "pending")
->where('user_id','=', $user_id)
->where(DB::raw('DATE_ADD(created_at, INTERVAL 1 HOUR) > NOW()'))
->groupBy('status')
->get();
But I'm not getting wrong response
Anybody help me!!
Just get last row of this user on table and get created_at column?
Then make another query to get count requests by this user in the our on the last 'created_at`query value.
So you can know if this our has 1, 2, 3, 4 or five requests.
Is very simple. ;)
I am working with the fullcalendar plugin and I am trying to fetch all the events in my database after and before a certain date. Full calendar paginates the events by adding paramaters called 'start' and 'end'. I am able to parse these in to Carbon instances like so:
Carbon::parse($data['start']);
Carbon::parse($data['end']);
Now this is working fine:
$events = $this->event->where('user_id', auth()->user()->id)
->get();
But this is not:
$events = $this->event->where('user_id', auth()->user()->id)
->where('start', '>=', Carbon::parse($data['start']))
->where('end', '<=', Carbon::parse($data['end']))
->get();
Why am I not getting events published between these two dates with the second approach. When I run the first approach, it works fine and fetches all the events and publishes them on the current page but unfortunately it is also fetching a whole lot of events that don't need fetching.
After listening to the DB queries, the event query was like:
select * from events where user_id = ? and start >= ? and end <= ?
This was a small mistake on my end where I was not setting the end date for the event in my DB and hence the query where all the events for the authenticated users was working correctly.
In fact the query for >= start would also have worked alone but the the <= end was creating as issue since end was null and hence Eloquent was not returning any models.