I launch a website that stores images and users get a hot link.
I want to be able to query the records made over the past 7 days in the table containing the data of the uploaded image, extract only the created_at column and compile the data into an array, similar to creating a list of archives for a blog.
I would like the results to be presented as follows:
[ 'Sunday' => 5, 'Monday' => 45, 'Tuesday' => 452, ... ]
Where each number represents the number of entries created on each day. While I can output such an array, I can easily handle Javascript.
Anyone have any suggestions?
EDIT
This is the code I've tried so far:
<?php class Admin { public function getCreatedAtAttribute($value) { $this->attributes['created_at'] = Carbon::createFromFormat('Ymd H:i:s', $value); } public static function uploadsGraph() { $date = \Carbon\Carbon::now(); $uploads = Upload::select('created_at')->where('created_at', '>=', \Carbon\Carbon::now()->subWeek())->get(); foreach($uploads as $date) { echo $date->created_at . '<br>'; } } }
EDIT 2
Here is another version I tried, but that didn't work.
class Admin { public static function uploadsGraph() { $date = \Carbon\Carbon::now(); $uploadsByDay = DB::table('uploads') ->select(DB::raw(' YEAR(created_at) year, MONTH(created_at) month, MONTHNAME(created_at) month_name ')) ->groupBy('year') ->groupBy('month') ->orderBy('year', 'desc') ->orderBy('month', 'desc') ->get(); dd($uploadsByDay); } }
source share