jeudi 19 novembre 2015

Laravel Eloquent : Record of Records for model instance

I'm trying to figure out what's the best way of getting the index of the given Model in the Collection.

Scenario : list of videos, which belong to a specific title. Title can have one or more videos. When on the given video page with the Video model at hand, I would like to display something along the lines Video ? of ?.

Basically what I need to achieve is something that would reflect the following, raw sql statement:

SELECT `v`.*
(
    SELECT COUNT(`id`)
    FROM `videos`
    WHERE `title_id` = ?
    AND `identity` <= `v`.`identity`
) AS `collection_index`
FROM `videos` `v`
WHERE `v`.`id` = ?

Where identity is simply some field that is used to sort videos - might be numerical but also string in the format 01-03 etc. In the above collection_index would store the index (or rather sequential position) of the given record in the collection.

Is it possible with a single statement on the Eloquent model - anyone came across a similar problem?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire