Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

garrettmassey's avatar

Spatie ActivityLog: Get most recent activity by causer

Hello!

When using the Spatie package spatie/laravel-activitylog, you can access all activity by Activity::all() and the latest activity by Activity::latest(), but I cant find anything in the documentation about getting activity by a causer. What I am trying to do is get the most recent (latest) activity logged by a given causer (in this case, User, but some activity is logged by events, so not all causers are of the type App\Models\User).

What I have tried in Tinker is this:

use Spatie\Activitylog\Models\Activity;
$user = App\Models\User::find(1);
$user_latest = Activity::by($user)->latest();

and:

use Spatie\Activitylog\Models\Activity;
$user = App\Models\User::find(1);
$user_latest = Activity::where('causer_type', 'App\Models\User')->where('causer_id', $user->id)->latest();

and neither of those seems to work. Does anyone know if there is a specific method for accessing logs that are causedBy` a specific model instance, like a specific user? I couldn't figure it out in the documentation, and I can't seem to figure out how to build the eloquent query.

0 likes
1 reply
garrettmassey's avatar
garrettmassey
OP
Best Answer
Level 6

I think I figured it out...

$user_latest = Activity::where('causer_type', 'App\Models\User')->where('causer_id', $user->id)->latest('created_at')->first();

This seems to give me the results I want, even if it's not quite as elegant as I had hoped.

Please or to participate in this conversation.