Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

david2000's avatar

Adapt my code by adding start_time and stop_time in my booking

I have to add in my TrainingController two variables => start_time & stop_time.

In my old code I had this:

 $conflictTraining = Training::where('fk_motorbike', $request->get('fk_motorbike')) 
 ->whereDate('date_seance', "=" , Carbon::parse($date_seance)) 
 ->where('hour_start', "<=" , $request->get('hour_start')) 
 ->where('hour_end', ">=" , $request->get('hour_end'))
 ->where('fk_former', $request->get('fk_former'))
 ->first();

My problem is that I would like to do a checking. How Can I avoid a duplicate for my request $conflictTraining with start_time & stop_time..

Here is my code for now:

public function store(Request $request)
    {
        $request->validate([
            'date_seance' => 'required',
            'hour_start' => 'required',
            'hour_end' => 'required',
            'fk_motorbike' => 'required',
            'fk_former' => 'required',
            'fk_student' => 'required',
            'fk_typeseance' => 'required'
 
 
        ]);

        
        $start_time = Carbon::createFromFormat('d-m-Y H:s', $date_seance . ' ' . $hour_start);
        $stop_time = Carbon::createFromFormat('d-m-Y H:s', $date_seance . ' ' . $hour_end);

    
        $conflictTraining = Training::where('fk_motorbike', $request->fk_motorbike)
            ->where('start_time', "<=", $start_time)
            ->where('stop_time', ">=", $stop_time)
            ->first();
 
 
        if (isset($conflictTraining)) {
            return redirect()->route('trainings.index')
                ->with('error', 'training duplicate');
        }
 
      
        $data = $request->all();
        $data['start_time'] = $start_time;
        $data['stop_time'] = $stop_time;
        Training::create($data);
        return redirect()->route('trainings.index')
            ->with('success', 'Add');
 
 
    }

I thank you in advance for your help.

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

don't know what you mean?

When checking for conflict, you are looking for models that have;

  • start between $start_time and $end_time, or
  • end between $start_time and $end_time, or
  • start earlier than $start_time AND end later than $end_time

Your code also has an issue in that you use $date_seance and $hour_start/end before initialising them

1 like
kropcik's avatar

Dunno if its a best practice, but on our company we are doing it simple and making three checks.

For example, lets think you have reserved resort from 2018/05/05 to 2018/05/10. And you want to extract free/busy periods. And your client gives you DATE_FROM and DATE_TO (the period) which he would like to take.

...........DATE_FROM.........2018/05/05..................DATE_TO........................2018/05/10...............

.........................................2018/05/05..................DATE_FROM...................2018/05/10..........DATE_TO......

..........DATE_FROM..........2018/05/05..........................................................2018/05/10..........DATE_TO......

select * from 'reservations' where 'unit_id' = '1' and (('date_from' <= '2018-05-01 08:00:00' and 'date_to' >= '2018-05-01 08:00:00') or ('date_from' >= '2018-05-01 08:00:00' and 'date_to' <= '2018-05-17 08:00:00') or ('date_from' <= '2018-05-17 08:00:00' and 'date_to' >= '2018-05-17 08:00:00')) and ('status' = '1' or 'status' = '0') ......

You filter through all the reservations (or atleast +- in the period) and if you get atleast one result from this kind of query, it means that unit/resort has a reservation intersection for the period. I think I made myself clear, still I dont take this as "best practice" :)

Please or to participate in this conversation.