Stephen H. Anderson
Stephen H. Anderson

Reputation: 1068

Recent activity page in Laravel

We have a big system with hundreds of models (Users, Employees, Payments, Tasks, Projects, and so on).

We would like to implement a main page for our system that displays the latest activity (a Facebook-like page). This main page would be very simple and display something like:

Project #123 modified by Frank Smith (1 hour ago).

Task #129 created by John Cook (1 day ago).

etc.

Each of these will link to the modified model. Is there an "easy" way to implement this without having to spread code through all our system?

I'm sure Laravel has something that would make implementing this very simple. But can't seem to find a solution.

Upvotes: 0

Views: 67

Answers (3)

Devon Bessemer
Devon Bessemer

Reputation: 35347

Define a common interface for the models that you care about to make sure you can pull the id, name, or any other information from all the models. (Edit: You probably don't need the model to define the user and timestamp since that can be pulled from the current timestamp and the current auth user).

Define an event listener for 'eloquent.saved' that checks if the model implements that interface and creates the feed "post" with that information.

Upvotes: 2

Khaldoun Nd
Khaldoun Nd

Reputation: 1466

I haven't personally used it yet (will do very soon) but i thought this library might suit your needs: https://github.com/spatie/laravel-activitylog

I hope it works for your case

Upvotes: 0

George Hanson
George Hanson

Reputation: 3040

I would personally go down the route of Laravel Model Observers. You can then save the events you would like to a table in the database and use that for your main page. You can read more about Model observers here - https://laravel.com/docs/5.8/eloquent#observers

Upvotes: 1

Related Questions