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

david2000's avatar

checking date payment for my function update()

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.

0 likes
14 replies
Nakov's avatar

And what is $date_seance in your store and your update method? You can try to see the difference by dumping the value before you use it:

dd($date_seance);

What does this returns in both cases?

david2000's avatar

In the store I retrieve the date => "2019-10-25".

In my update, I want to change the date "2019-10-25", I retrieve "2019-10-26".

Nakov's avatar

So I don't understand what do you mean by this:

My problem is in my function update(), I am stuck. When, I change the value of the date_seance, there is no checking.

In your database do you have payments for the student that you want to update, and the payment date is after 2019-10-26?

Because this code isset($datePayment) will return false if the result from the query and first() returns null.

david2000's avatar

Here is an example with 2 screenshot.

In my listing payment I have a field date_payment with the value 15/10/2019.

Then, in my listing training I have a field date_seance with the value 25/10/2019

If the user wants to change the value of the date_seance by 10/10/2019, the date_seance is smaller that the date_payment. I want to have an error message.

My 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! ');
}

I am stuck for the method update() ... :-(

Nakov's avatar

@david2000 Can you please show your full method code of the update function + your edit view?

david2000's avatar

Hello @nakov,

Here is my code... My condition between my date_seance and date_payment is ok for the method store(), however I am stuck in my method update.

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'
                
                
        ]);


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



        $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{
            Training::create($request->all());
                return redirect()->route('trainings.index')
                    ->with('success', 'Add');
        }

       

    }

   
    public function edit($id)
    {
        $trainings = Training::find($id);
        $students = Student::all();
        $formers = Former::all();
        $motorbikes = Motorbike::all();
        $payments = Payment::all();

        return view('admin.trainings.edit', compact('trainings', 'students', 'formers', 'motorbikes', 'payments'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
                'date_seance' => 'required',
                'hour_start' => 'required',
                'hour_end' => 'required',
                'fk_motorbike' => 'required',
                'fk_former' => 'required',
                'fk_student' => 'required'
        ]);

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



        $datePayment = Payment::where('fk_student', $request->get('fk_student'))
        ->whereDate('date_payment', ">" , $date_seance)
        ->first();



        $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_motorbike = $request->get('fk_motorbike');
        $trainings->fk_former = $request->get('fk_former');
        $trainings->fk_student = $request->get('fk_student');
        $trainings->save();
        return redirect()->route('trainings.index')
            ->with('success', 'Update!')->withInput();
     
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $trainings = Training::find($id);
        $trainings->delete();
        return redirect()->route('trainings.index')
                    ->with('error', 'Delete! ');
    }


File Edit

<form class="panel-body" action="{{route('trainings.update',$trainings->id)}}" method="POST">
              <input name="_method" type="hidden" value="PATCH">
              @csrf
                <fieldset class="form-group {{ $errors->has('date_seance') ? 'has-error' : '' }}">
                <label for="form-group-input-1">Date séance</label>
                <input type="date" name="date_seance" id="date_seance" class="form-control" required="required" value="{{$trainings->date_seance->format('Y-m-d')}}">
                {!! $errors->first('date_seance', '<span class="help-block">:message</span>') !!}
          </fieldset>

          <fieldset class="form-group {{ $errors->has('hour_start') ? 'has-error' : '' }}">
                <label for="company-content">Hour start</label>
                  <select class="form-control" name="hour_start">
                      <option value="07:00" @if (old('hour_start') == '07:00') selected="selected" @elseif($trainings->hour_start == '07:00') selected="selected" @endif>07:00</option>
                      <option value="10:00" @if (old('hour_start') == '10:00') selected="selected" @elseif($trainings->hour_start == '10:00') selected="selected" @endif>10:00</option>
                      <option value="13:00" @if (old('hour_start') == '13:00') selected="selected" @elseif($trainings->hour_start == '13:00') selected="selected" @endif>13:00</option>
                      <option value="16:00" @if (old('hour_start') == '16:00') selected="selected" @elseif($trainings->hour_start == '16:00') selected="selected" @endif>16:00</option>
                  </select> 
              </fieldset>

          <fieldset class="form-group {{ $errors->has('hour_end') ? 'has-error' : '' }}">
                <label for="company-content">Hour end</label>
                  <select class="form-control" name="hour_end">
                      <option value="09:00" @if (old('hour_end') == '09:00') selected="selected" @elseif($trainings->hour_end == '09:00') selected="selected" @endif>09:00</option>
                      <option value="12:00" @if (old('hour_end') == '12:00') selected="selected" @elseif($trainings->hour_end == '12:00') selected="selected" @endif>12:00</option>
                      <option value="15:00" @if (old('hour_end') == '15:00') selected="selected" @elseif($trainings->hour_end == '15:00') selected="selected" @endif>15:00</option>
                      <option value="18:00" @if (old('hour_end') == '18:00') selected="selected" @elseif($trainings->hour_end == '18:00') selected="selected" @endif>18:00</option>
                  </select> 
              </fieldset>

            <fieldset class="form-group {{ $errors->has('fk_motorbike') ? 'has-error' : '' }}">
               <label for="company-content">Number motorbike </label>
              <select name="fk_motorbike" id="" class="form-control">
                  @foreach($motorbikes as $motorbike)
                    <option value="{{$motorbike->id}}" @if($trainings->fk_motorbike == $motorbike->id) selected @endif > {{$motorbike->number_motorbike}}  
                    </option>
                @endforeach
              </select>
            </fieldset>

            <fieldset class="form-group {{ $errors->has('fk_former') ? 'has-error' : '' }}">
               <label for="company-content">Former </label>
              <select name="fk_former" id="" class="form-control">
                  @foreach($formers as $former)
                    <option value="{{$former->id}}" @if($trainings->fk_former == $former->id) selected @endif >{{$former->name}} 
                    </option>
                @endforeach
              </select>
            </fieldset>

             <fieldset class="form-group {{ $errors->has('fk_student') ? 'has-error' : '' }}">
               <label for="company-content">Name student </label>
              <select name="fk_student" id="" class="form-control">
                  @foreach($students as $student)
                    <option value="{{$student->id}}" @if($trainings->fk_student == $student->id) selected @endif >{{$student->name}} {{$student->firstname}}  
                    </option>
                @endforeach
              </select>
            </fieldset>
Nakov's avatar

@david2000 So in your update method I don't see the check at all..

if($datePayment) 
{
    return redirect()->route('trainings.index')
                ->with('error', 'There is a problem with the payment date! ');
}

Where is this?

david2000's avatar

@nakov : I tried this, but my checking does not work. I think it's a problem with ID? Is my "if" condition is placed correctly?

public function update(Request $request, $id)

    {

        $request->validate([

                'date_seance' => 'required',

                'hour_start' => 'required',

                'hour_end' => 'required',

                'fk_motorbike' => 'required',

                'fk_former' => 'required',

                'fk_student' => 'required'

        ]);



       $date_seance = $request->get('date_seance');

       $hour_start = $request->get('hour_start');

       $hour_end = $request->get('hour_end');

       $fk_motorbike = $request->get('fk_motorbike');

       $fk_student = $request->get('fk_student');

       $fk_former = $request->get('fk_former');

       $fk_typeseance = $request->get('fk_typeseance');



       $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();
   }

    }

Nakov's avatar

@david2000 you don't use the $id before the check, why would it be a problem?

Your problem is that this code:

$datePayment = Payment::where('fk_student', $request->get('fk_student'))
->whereDate('date_payment', ">" , $date_seance)
->first();

returns null. So you need to check is this: $request->get('fk_student') and this $date_seance the correct ones, you can dump them to test:

dd($request->get('fk_student'), $date_seance);

And see what that prints out. Then check in your database if you have a record that is after this date.. if you don't then it means that it will go to the else block where it should update the $training.

To test if the $id is the problem, then just print out what does it return: dd($trainings below the Training::find($id); line.

NOTE Can you also share your route.. do you have this in the routes file:

Route::patch('/training/{id}/update', 'TrainingsController@update')->name('trainings.update');

??

david2000's avatar

Hekki @nakov ,

Concerning the dump below:

dd($request->get('fk_student'), $date_seance);

For the test

Training::find($id); 

I have this:

My dateFormat is null ???

In the routes file, I have this:

Route::resource('/trainings', 'TrainingController');
Nakov's avatar

@david2000 dateFormat can be null it has nothing to do with your problem now. What I fail to understand is what is going on :) when you say you are stuck the update doesn't work, what do you mean by that?

Do you have in the DATABASE a payment record that is after '2019-10-11' for the student with ID 1?

Everything seems correct and yet you have a problem which I don't understand what it is?

david2000's avatar

I explain ... If the date_payment is 10/10/2019. Then I enter the date_seance 12/10/2019. Here, there is no problem.

Now, if I change the value 12/10/2019 by 01/01/2018 there must be a message that says the date_seance must be later the date_payment.

I don't know how to do this check in my update() ... :-(

Nakov's avatar
Nakov
Best Answer
Level 73

@david2000 your code should work then.

But when I asked you to print out the student id and the date_seance:

dd($request->get('fk_student'), $date_seance);

You said that the $date_seance returns 2019-10-11 which is not what you are saying now, so if the payment date is 2019-10-10 that's not after the 2019-10-11 so make sure that when you change the date in the field, you are sending the correct one to the back-end.

david2000's avatar

I have found my erreur it was the cache pfffffff... :-(

php artisan config:cache
php artisan cache:clear
php artisan view:clear

It woks now !

Thank you for your help.

Please or to participate in this conversation.