score:1

Accepted answer

$value->created_at is a full date/time string. since you want to organize it by dates, then you'll need to format it first. thankfully, laravel's timestamps are automatically converted to carbon, so it's easy to format

$attrs[$value->created_at->todatestring()][] = $value->order;

if you want just the year/month, then use format() instead

$attrs[$value->created_at->format('y-m')][] = $value->order;

edit i see now you're using query builder (db), not eloquent, so you'll need to actually parse the timestamp separately.

$date = carbon::create($value->created_at);
$attrs[$date->todatestring()][] = $value->order; // 2021-07-14
$attrs[$date->format('y-m')][] = $value->order;  // 2021-07

score:0

if you are using collection then

 $user=db::table('analytics')->get()
                            ->maptodictionary(function ($analytics){
                                $date=carbon::parse($analytics->created_at)->format('d-m-y');
                                $data[$date]=$analytics->order;
                                return $data;
                             })->toarray();

score:0

my recommendation would be to use eloquent instead of the db query builder. if you have an analytic model, then this is quite simple. you could just add created_at to your $dates array and it will automatically be cast as a carbon instance. for instance, in your model:

protected $dates = ['created_at'];

Related Query

More Query from same tag