Boby
Boby

Reputation: 77

Laravel Update Request MethodNotAllowedException

I have a class called CustomerController with an Update function:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
//use app\model\Customer;
use Illuminate\Support\Facades\View;
use App\model\Customer;

class CustomerController extends Controller
{
   public function getAllCustomer()
   {
       return Customer::get();
   }

public function addNewCustomer(Request $request)
{
    $validatedData = $request->validate([
        'Title' => 'required',
        'Name' => 'required|max:255',
        'Surname' => 'required|max:255',
        'Email' => 'required',
        'Phone' => 'required',
        'Password' => 'required',
        'dateofBirth' => 'required'
    ]);

    return \app\model\Customer::create($request->all());
}


public function update (Request $request , Customer $id)
{
    $id->update($request->all());
}

This is the route:

Route::put('Customer/{id}' , 'CustomerController@update');

But currently Im getting a MethodNotAllowedException, I couldnt find any solution. A screenshot: enter image description here Thank you very much!

Route does exist: enter image description here

Upvotes: 0

Views: 71

Answers (2)

Munch
Munch

Reputation: 779

When updating, therefore using the PUT method, you must have a hidden input in your form like the below:

<input type="hidden" name="_method" value="PUT">

the form will still be a post

<form action="/" method="POST">

Or as mentioned by @kerbholz (many thanks) you can use the helper

{{ method_field('PUT') }}

Upvotes: 2

DsRaj
DsRaj

Reputation: 2328

change you route method with both put and patch

The main reason you're getting this error is that your form submit method is different with your route method.

Route::match(['put', 'patch'], '/Customer/{id}','CustomerController@update');

Upvotes: 0

Related Questions