I would like to display in a table the name of the user as well as the number of photos posted during the current week. Example : User1 has posted 10 photos, User2 has posted 20 photos...
So I need a count, but I'm not sure how.
Picture model :
public function user()
{
return $this->belongsTo('App\Models\User');
}
User model :
public function pictures()
{
return $this->hasMany('App\Models\Picture');
}
Query in the controller :
$users = User::whereHas('pictures',
fn($q) => $q->where('created_at', '>=', Carbon::now()->subDays(7)->toDateTimeString()))
->limit(10)
->get();
In the view:
{{ $user->photos->count() }}
Except that it counts all the photos of the user and not just the photos for the past week. How can I only count the recent photos?