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

david2000's avatar

Adapt my method store () for update ()

Here is my method store(). there is a check on the ceiling not to be reached.

For information, the code is correct for the method show().

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


       $date_seance = $request->get('date_seance'); 
       $hour_start = $request->get('hour_start'); 
       $hour_end = $request->get('hour_end'); 
       $fk_student = $request->get('fk_student');

        $thisStudentsTrainings = Training::where('fk_student', $fk_student)->get();

        $thisStudentsPayments = Payment::where('fk_student', $request->get('fk_student'))->get();


        if(count($thisStudentsTrainings) >= count($thisStudentsPayments) * 5) {
            return redirect()->route('trainings.index') 
                ->with('error', 'ceiling reached!'); 
        }


        else{
            Training::create($request->all());
                return redirect()->route('trainings.index')
                    ->with('success', 'Add');
        }

       
    }

I want to create my method update () but I am stuck ... I don't understand how I could adapt my code of my method store in my method update() ?

public function update(Request $request, $id)
{  

   
   $request->validate([
                'date_seance' => 'required',
                'hour_start' => 'required',
                'hour_end' => 'required',
                'fk_student' => 'required',
      
               
        ]);

 
   if{
       $trainings = Training::find($id);
       $trainings->date_seance = $request->get('date_seance');
       $trainings->hour_start = $request->get('hour_start');
       $trainings->hour_end = $request->get('hour_end ');
       $trainings->fk_student = $request->get('fk_student ');
       $trainings->save();
       return redirect()->route('trainings.index')
              ->with('success', 'Update!')->withInput();

   }

}


0 likes
1 reply
geowrgetudor's avatar
Level 4

First, the $request->validate() method returns an array so this

$request->validate([
                'date_seance' => 'required',
                'hour_start' => 'required',
                'hour_end' => 'required',
                'fk_student' => 'required'
                
                
        ]);


       $date_seance = $request->get('date_seance'); 
       $hour_start = $request->get('hour_start'); 
       $hour_end = $request->get('hour_end'); 
       $fk_student = $request->get('fk_student');

should look like this

$form = $request->validate([
                'date_seance' => 'required',
                'hour_start' => 'required',
                'hour_end' => 'required',
                'fk_student' => 'required'
                
                
        ]);


       $date_seance = $form['date_seance']; 
       $hour_start = $form['hour_start']; 
    // and so on ...

2nd thing is you shouldn't use ->get() if you need only to count stuff. So this

$thisStudentsTrainings = Training::where('fk_student', $fk_student)->get();

        $thisStudentsPayments = Payment::where('fk_student', $request->get('fk_student'))->get();

should be like this

$thisStudentsTrainings = Training::where('fk_student', $form['fk_student'])->count();

        $thisStudentsPayments = Payment::where('fk_student', $form['fk_student'])->count();

 if($thisStudentsTrainings >= $thisStudentsPayments * 5) {
            return redirect()->route('trainings.index') 
                ->with('error', 'ceiling reached!'); 
        }

As for your update() method, it should be pretty much the sames as in the store() method, because i guess he's only allowed only to change the date and time when the training will happen... so that means he won't add a new training and if updating it doesn't affect the payment you shouldn't check for that again.

1 like

Please or to participate in this conversation.