Reputation: 971
I have to do a checking between the payment date and the seance date in my method store(). The method store() is OK!
$datePayment = Payment::where('fk_student', $request->get('fk_student'))
->whereDate('date_payment', ">" , $date_seance)
->first();
if(isset($datePayment)){
return redirect()->route('trainings.index')
->with('error', 'There is a problem with the payment date! ');
}
My problem is in my function update(), I am stuck. When, I change the value of the date_seance, there is no checking.
$datePayment = Payment::where('fk_student', $request->get('fk_student'))
->whereDate('date_payment', ">" , $date_seance)
->first();
if(isset($datePayment)){
return redirect()->route('trainings.index')
->with('error', 'There is a problem with the payment date! ');
}
else{
$trainings = Training::find($id);
$trainings->date_seance = $request->get('date_seance');
...
$trainings->save();
return redirect()->route('trainings.index')
->with('success', 'Update!')->withInput();
}
I know my problem is here in the condition:
if(isset($datePayment)){
return redirect()->route('trainings.index')
->with('error', 'There is a problem with the payment date! ');
}
Thank you for your help.
Upvotes: 1
Views: 59
Reputation: 8178
If this is inside of an update($id)
method, the change to the date_seance
will have no checking because the query is looking for the wrong student's payments.
Before you look for your payment check, get the training model that is being updated inside the method (from the $id
that was passed in). This will have the correct student id
from it's model. So..
In the update method:
$training = Training::find($id);
$datePayment = Payment::where('fk_student', $training->fk_student) // NOT from $request
->whereDate('date_payment', ">" , $date_seance)
->first();
Instead of pulling in the student who is writing the form, make sure you are getting the payments from the student who is on the payment.
Upvotes: 2