4 months ago

Order records by field, get record before or after any given

Posted 4 months ago by hunterhawley

Hey y'all, I've got a model called 'Substitution", and it has a field called 'video_timestamp' which is an integer. I'm ordering them by this field, and cycling through them in a foreach in one of my views, but I would like to be able to get a field from a record that is one record after whichever one I am at in the foreach. For instance:

Record 1: 'video_timestamp' = 100, 'other_field' = 'hello'; Record 2: 'video_timestamp' = 230, 'other_field' = 'there'; Record 3: 'video_timestamp' = 600, 'other_field' = 'world';

So lets say in the foreach statement, I am currently at Record 2. I want to be able to access the 'hello' from Record 1. However, it isn't a given that the ID for Record 1 will be one fewer than Record 2's. So, I am trying to move around only in the orderBy('video_timestamp') list that I created in my controller. How might I go about doing this?

Please sign in or create an account to participate in this conversation.