Search code examples
phplaravelcontrollerviewactivitylog

Spatie Activity Log only returns the last log. I want all logs returned.


So, I went to read all the Spatie Activity Log documentation and in there examples it only returns the most recent log (One last log). But I need to see all the Activity Log created.

     public function index()
    {
    $user = Auth::user(); 
    Activity()->log('Look mum, I logged something') ->causedBy($user);
    $lastActivity = Activity::all()->last();
    $lastActivity =$lastActivity->causer;
    $lastActivity->description;
    return View('hello', compact('lastActivity'));
    }

I think it has to do with hte Activity::all()->last(); I tried deleting the last but it won't work. Thank you very much for your help.


Solution

  • Activity is an Eloquent model, to get all the activities you can call Activity::all();, it will returns a collection.

    public function index()
    {
        $user = Auth::user(); 
        activity()->log('Look mum, I logged something')->causedBy($user);
    
        $allActivities = Activity::all();
        return view('hello', compact('allActivities'));
    }
    

    Then in your hello view, you can loop through the collection

    @foreach($allActivities as $activity)
    {{ $activity->description }}
    @endforeach