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:
You are using paginate which restricts the number of results fetched from the DB. If you want to get previous 10 days activity of a user then you can write your query as:
$activity = $this->where('user_id', '=', $userId)
->where('created_at', '>=', \Carbon\Carbon::now()->subDays(10))
->get();