I am using https://github.com/owen-it/laravel-auditing which logs every change to every model (which uses a special trait) to a table called audits
:
id
auditable_type
= name of the model class which was changedauditable_id
= id of the model which was changedcreated_at
= date of change- ...
With the following example data:
| id | auditable_type | auditable_id | created_at |
|----|----------------|--------------|------------|
| 1 | App\City | 1 | 2019-01-01 |
| 2 | App\City | 2 | 2019-01-02 |
| 3 | App\City | 2 | 2019-01-02 |
Now I wanna query the two latest changes (order by created_at DESC
) of all models, which is pretty easy:
Audit::query()
->limit(2)
->orderBy("created_at", "DESC")
->get()
Which will result to this:
| id | auditable_type | auditable_id | created_at |
|----|----------------|--------------|------------|
| 2 | App\City | 2 | 2019-01-02 |
| 3 | App\City | 2 | 2019-01-02 |
But how to get the latest changed model rows, not the latest changes:
| id | auditable_type | auditable_id | created_at |
|----|----------------|--------------|------------|
| 1 | App\City | 1 | 2019-01-01 |
| 3 | App\City | 2 | 2019-01-02 |
Normally, I would use GroupBy, but thats not possible (directly) on eloquent models, right?