afujita's avatar

How to retrieve a value from the previous record

Hi. I trying to build a dynamic event timetable, using laravel 4.2 and CRUD. The table has basically

| id | start_time | duration | end_time | performer |
|----|------------|----------|----------|-----------|
|  1  |   9:00   | +15 minutes   |  9:15  |  Performer A   |

After some research, I found a way to retrieve the id, which is commonly used in paginations, I think. Here is the code:

$previous = Time::where('id', '<', $time->id)->max('id');

But in my case, I need to retrieve the "end_time" column value from the previous id into my CREATE view page, to know the next start_time and than input the "duration" . The end_time is filled dynamically by adding the duration time with the start_time.

Appreciate any help!

0 likes
10 replies
pmall's avatar

I need to retrieve the "end_time" column value from the previous id

Not sure to understand why, can you explain?

1 like
tomopongrac's avatar
Level 51

@afujita

Maybe you can something like this

$previous = User::where('id', '<', $user->id)->orderby('id', 'desc')->first();
afujita's avatar

@pmall

Because the "end_time" of #id1, that is 9:15, will be the "start_time" of #id2. That is, previous "end_time" will be the next "start_time".

And cause I didn't find the solution yet, I coded this:

$time = Time::find($id);

$time->start_time1 = Input::get('start_time1');
$time->duration1 = Input::get('duration1');
$start1 = strtotime($time->start_time1);
$time->end_time1 = date("H:i", strtotime($time->duration1, $start1));
$time->performer1 = Input::get('performer1');

$time->duration2 = Input::get('duration2');
$time->start_time2 = $time->end_time1;
$start2 = strtotime($time->start_time2);
$time->end_time2 = date("H:i", strtotime($time->duration2, $start2));
$time->performer2 = Input::get('performer2');

and so on….

The problem is that I have about 50 rows.

nfauchelle's avatar

@afujita

I think your missing some more details. Timetable should be storing a date as well, since you wont use the same timetable for each day?

You can use max('end_time'); to get the current oldest end time, but... I think to help you better we need to see tables for Timetable and also the Time models

Also a bit of a rough idea of what your trying to do.

Your code above, it doesn't help much.

1 like
pmall's avatar

Yes why not just max('end_time'); ?

1 like
nfauchelle's avatar

@afujita Caution... If you delete a couple of timeslots and then add some in for earlier in the day, the order is going to be squiff. A higher id doesn't mean a older end_time.

Suggest you order by end_time

1 like
afujita's avatar

@nfauchelle

Thank you very much for your idea. Actually I have other columns, like date (day_id). Just simplified the example. I will try with your suggestion also, the max('end_time');

My solution is working, but any adjustment is being tough, cause I have many columns

Here is a part of my model:

class Time extends Eloquent {

    public function day() {
        return $this->belongsTo('Day');
    }

    public function performers() {
        return $this->belongsTo('Performer');
    }

}
nfauchelle's avatar

@afujita

Wouldn't Time not have a Day relation, but a Timetable relation?

The way I would see it is like this

Model: Timetable
    date
    has_many times (Time model) -> order_by end_time asc

Model: Time    
    has_one timetable (Timetable model)
    belongs_to performers (Performer model)
    start_time
    duration
    end_time

The time table wouldn't have 50 columns, or start_time1 start_time2 start_time3 etc

You could then have a helper on the Timesheet model like - oldestEndTime which did return $this->times()->max('end_time'); which would make things easier.

So when you display the times for the day, you just loop over the times on the Timesheet.

But without seeing more, it's hard to say :)

(ps, I would probably call Time TimeEntry or TimeSlot)

1 like
afujita's avatar

Appreciate your time @nfauchelle

Your solution seems to be really fine. Let me try this too.

Please or to participate in this conversation.