I have a database in which I store documents - some are independant from each other, but some are just a "next version" of a previous document. In that case, I store the id of the latest version in a column.
An example dataset would be like this:
id | name | version | validated | latest_version_id
1 | 'Doc 1' | 1 | true | null
2 | 'Doc 2' | 1 | true | null
3 | 'Doc 3 v1' | 1 | true | 5
4 | 'Doc 3 v2' | 2 | true | 5
5 | 'Doc 3 v3' | 3 | false | 5
I would like to be able to select all validated documents in their latest version. Typically a request that would return lines 1, 2 and 4 in the dataset.
I was hoping to be able something like Docs::where('validated', true)->distinct('latest_version_id')->get()
but it's obviously not as simple as that..
Any query guru to point me in the right direction? A solution would be to get all results and then filter out some models, but that doesn't sound very optimized.
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2OFaaqV
via IFTTT
Aucun commentaire:
Enregistrer un commentaire