vendredi 18 octobre 2019

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 check how Can I avoid a duplicate for my request $conflictTraining 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.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/35IcFyR
via IFTTT

Aucun commentaire:

Enregistrer un commentaire