Laravel sort the result are grouped by weeks
$byweek = Reservation::all()->groupBy(function($date) {
return Carbon::parse($date->check_in)->format('W');
});
$byweek = $byweek->reverse();
Done
Get the all records and then reverse the collection to get the reverse 17, 16, 15 etc.. order.
Laravel Eloquent, group by week and display start and end of week
Yes, with Carbon it is easy to find the start and end of week.
->groupBy(function($date) {
$created_at = Carbon::parse($date->created_at);
$start = $created_at->startOfWeek()->format('d-m-Y');
$end = $created_at->endOfWeek()->format('d-m-Y');
return "{$start} - {$end}";
})
Querying Data and Grouping by Day with Laravel
I'm assuming that the number next to each day of the week represents the number of records made on that day, with the entire dataset you want to query ranging only over the last 7 days.
The idea here is to select the count of items that were created on the same day (ignoring completely the timestamp portion of the created_at
column), so we can use DB::raw
inside of a select()
call to aggregate all of the entries that were created on a specific day and then restrict that dataset to only those created in the last week. Something like this ought to work:
$data = Upload::select([
// This aggregates the data and makes available a 'count' attribute
DB::raw('count(id) as `count`'),
// This throws away the timestamp portion of the date
DB::raw('DATE(created_at) as day')
// Group these records according to that day
])->groupBy('day')
// And restrict these results to only those created in the last week
->where('created_at', '>=', Carbon\Carbon::now()->subWeeks(1))
->get()
;
$output = [];
foreach($data as $entry) {
$output[$entry->day] = $entry->count;
}
print_r($output);
Also note that I assumed this to be a 'rolling' week, where if today happens to be a Thursday, then the first date in the dataset will be the previous Thursday. It will not start on the most recent Sunday, if that is what you need. If it is, you can change the -where()
condition to something like this:
...
->where('created_at', '>=', Carbon\Carbon::parse('last sunday'))
...
Laravel Group By Date
So guys, i have got the answer for this, Thank you for reading my question.
$orderbydate = DB::table('sales_flat_orders as w')
->select(array(DB::Raw('sum(w.total_item_count) as Day_count'),DB::Raw('DATE(w.created_at) day')))
->groupBy('day')
->orderBy('w.created_at')
->get();
The Raw query in my query was wrong for the created_at field. Now its good. Hope you will also get some input from this.
Related Topics
Send Python Output to HTML Field
Generate an N-Digit Random Number
Install PHP Intl Extension on Macos
Get Image Type from Base64 Encoded Src String
How to Change Value of Select Options Based on Another Select Option Selected
Getting a File to Download Instead of Opening the Browser
Mysql Return Result If 0 Rows Returned
Option Selected With Codeigniter and Data from Db
How to Get Username from Facebook Id
Laravel - Display a Pdf File in Storage Without Forcing Download
Check If Username and Email Already Exists in Database
How to Download an Mp3 File from Remote Url
How to Get the Sucess Message in the Same Page After Submitting the Contact Form
What Are Long-polling, Websockets, Server-sent Events (Sse) and Comet