vendredi 29 décembre 2017

Laravel get only models with current version

I have implemented custom model versioning in Laravel Framework. There is couple of columns handling it in the database table: sibling_id (links model's different versions together), version, accepted and validFrom.

For example:

| id | sibling_id | version | accepted | validFrom  | ... |
|----|------------|---------|----------|------------|-----|
| 1  | 1          | 1       | 1        | 2017-12-01 |     |
| 2  | 1          | 2       | 1        | 2018-06-01 |     |
| 3  | 2          | 1       | 1        | 2017-12-10 |     |
| 4  | 2          | 2       | 0        | 2017-12-28 |     |
| 5  | 3          | 1       | 0        | 2017-12-01 |     |

What I mean with current model:

  • model having the biggest version number within accepted and valid modals
  • OR version 1 if there isn't any other versions

I would like to have some way to get normally only current model grouped by sibling_id (so within this example the collection should include models 1, 3 and 5). This should work like same way than Laravel's withTrashed() function so that I can include all if I want to (for example version history).

Maybe this can be done somehow with the scopes but I felt it complicated because of the "sibling grouping" and another complex filters so I ended up to ask here.

Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire