samedi 26 novembre 2016

Laravel - group data by created_at field and apply pagination

I need to get previous 10 days activity of a user. In my case i am just getting 10 activity records instead of previous 10 days activity.

*My Code:

$activity   = $this->where('user_id', '=', $userId)->paginate(10);

$activities = collect($activity->items())->groupBy(function($item) {
    return \Carbon\Carbon::parse($item->created_at)->format('d-M-Y');
});

$activity->setCollection($activities);

dd($activity->toArray());

Output:

enter image description here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire