Mohamed Midou
Mohamed Midou

Reputation: 29

Update injected model from route in Laravel

I am trying to update some values and it's creating a new one instead of updating the selected data

this is the controller code

public function update(Request $request, Payment_Student $payment_Student)
{        
   $payment_Student->date =request('date');
   $payment_Student->amount =request('amount');
   $payment_Student->formation_id =request('formation_id');
   $payment_Student->student_id =request('student_id');
   $payment_Student->save();
   return redirect()->route('payment.index');
}

This is my route code

Route::get('/home', 'HomeController@index')->name('home');
Route::get('/search','CategoryController@search');
Route::resource('/category','CategoryController');
Route::resource('/formation','FormationController');
Route::resource('/professor','ProfessorController');
Route::resource('/student','StudentController');
Route::resource('/classroom','ClassroomController');
Route::resource('/session','SessionController');
Route::resource('/payment','PaymentController');
Route::resource('/seance','SeanceController');
Route::resource('/paymentprof','PaymentProfessorController');
Route::resource('/paymentstudent','PaymentStudentController');
Route::resource('/presence','PresenceController');
Route::resource('/profile','ProfileController');

This is my balde view code

 <form method="POST" enctype="multipart/form-data" action="{{route('paymentstudent.update',$payment_Student->id)}}" class="form-horizontal">
  {{method_field('PATCH')}}
  @csrf
  Date:
   <br/>
  <input class="form-control" type="date" name="date">
  <br/>
  Amount:
  <input  type="number"  value="{{$payment_Student->amount}}"  name="amount" 
  class="form-control">
  <br/>
  Formation:
  <select class="form-control" name="formation_id">
        @foreach($formation as $formation)
    <option value="{{$formation->id}}">{{$formation->name}}</option>
        @endforeach
  </select>
  <br/>
  Student:
  <select class="form-control" name="student_id">
         @foreach($student as $student)
    <option value="{{$student->id}}">{{$student->lastname}}</option>
        @endforeach
  </select>
  <div class="card-footer">
         <button type="submit" class="btn btn-primary">Save Changes</button>
         </div>
  <br>
</form>

when I select a data to update it creates new data the updated value that I have inserted

Upvotes: 1

Views: 678

Answers (1)

Cl&#233;ment Baconnier
Cl&#233;ment Baconnier

Reputation: 6088

By default when you make a resource laravel will singularize the resource name and make a variable out of it.

Example: Route::resource('videos', 'VideoController') laravel will expect the variable $video

In your case, paymentstudent will probably give the variable $paymentstudent

Fortunatly, you can customize your own parameter name

Route::resource('paymentstudent', 'PaymentStudentController', ['parameters' => [
    'paymentstudent' => 'payment_Student'
]]);

Upvotes: 2

Related Questions