I am trying to get the time \"created_at\" for the last user activity,
I have the model User, and UserActivity.
I want to get the last user act
First, create another relationship in User model, which has to be hasOne to get the latest activity:
public function latestActivity()
{
return $this->hasOne(Activity::class)->latest();
}
Then load just users who have latest activities older than 3 days
$users = User::whereHas('activites', function($q) {
$q->where('created_at', '<=', now()->subDays(3));
})
->whereDoesntHave('activites', function($q) {
$q->where('created_at', '>', now()->subDays(3));
})
->with('latestActivity')
->get();