Home > Software design >  Encountering "404 NOT FOUND" for Laravel update/edit
Encountering "404 NOT FOUND" for Laravel update/edit

Time:10-28

in edit_customer:

<form action="{{ url('customer/profile_update', [ 'id'=> $cust->id ]) }}" method="POST" enctype="multipart/form-data">
   @csrf
   @method("PUT")

web.php //Customer related pages

Route::prefix('customer')->middleware(['auth','verified'])->name('customer.')->group(function(){
     Route::get('/',[HomeController::class,'index']);
     Route::get('cdash', Cdash::class)->name('cdash');
     Route::get('edit_customer/{id}',[Profile::class,'edit_customer'])->name('edit_customer');
     Route::put('update_customer/{id}',[Profile::class,'update_customer'])->name('update_customer');

Profile.php (controller)

public function edit_customer($id)
{
    $cust=  User::findOrFail($id);     
     return view('customer.edit_customer', compact('cust'));

 }

 public function update_customer(Request $request, $id)
 {
     $customer = User::find($id);

        
         //thebarber table details
         if($request->hasFile('image'))
         {
             $avatarpath= '/profileimages/'.$customer->image;

             if(File::exists($avatarpath))
             {
                 File::delete($avatarpath);
             }

             $file=$request->file('image');
             $ext=$file->getClientOriginalExtension();
             $filename=time().'.'.$ext;
             
             $file->move('/profileimages/',$filename);
             $customer->image=$filename;
         }
      
      
   
      $customer->firstname=$request->input('firstname');
      $customer->lastname=$request->input('lastname');
      $customer->email=$request->input('email');  
      $customer->phone=$request->input('phone');        
      $customer->city=$request->input('city');
      $customer->county=$request->input('county');
      $customer->country=$request->input('country');

      $customer->update();

      return redirect('/home')->with('message',"Customer Updated Sucessfully");

 }
 

CodePudding user response:

On edit_customer you are setting your method to PATCH and you are defining your route as PUT, please change one to match the other.

CodePudding user response:

You are using route group prefix “customer”, so your url in the form should be prefixed

url('customer/update_customer', [ 'cust' => $cust->id ]

https://laravel.com/docs/8.x/routing#route-group-prefixes

Route::prefix('customer')->middleware(['auth','verified'])->name('customer.')->group(function(){
 Route::get('/',[HomeController::class,'index']);
 Route::get('cdash', Cdash::class)->name('cdash');
 Route::get('edit_customer/{id}',[Profile::class,'edit_customer'])->name('edit_customer');
 Route::put('update_customer/{id}',[Profile::class,'update_customer'])->name('update_customer');

Your url for the from, to hit the update_customer function should be customer/update_customer/{id}

so this

<form action="{{ url('customer/profile_update', [ 'id'=> $cust->id ]) }}" method="POST" enctype="multipart/form-data">

should look like this

<form action="{{ url('customer/update_customer', [ 'id'=> $cust->id ]) }}" method="POST" enctype="multipart/form-data">

And a good practice is tou use route name instead of urls, so better to be

<form action="{{ route('customer.update_customer', [ 'id'=> $cust->id ]) }}" method="POST" enctype="multipart/form-data">
  • Related