Ajay Kumar
Ajay Kumar

Reputation: 11

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException no message in crud in laravel

i got an error when i trying to update a record, please some one help me !!

Form:

    {{Form::open(array('action'=>['MyController@update',$record->id]))}}

<div class="form-group"><label>Name</label><input type="text" value="{{$record->name}}" class="form-control" name="name"></div>
<div class="form-group"><label>Email</label><input type="text" value="{{$record->email}}" class="form-control" name="email"></div>
<div class="form-group"><label>Phone</label><input type="text" value="{{$record->phone}}" class="form-control" name="phone"></div>
<div class="form-group">
<input type="submit" class="btn btn-primary">
<input type="reset" class="btn btn-danger">
</div>
{{Form::close()}}

Route:

Route::resource("my","MyController");

Controller:

public function update(Request $request,$id){
    $record=Record::find($id);
    $record->name=$request->name;
    $record->email=$request->email;
    $record->phone=$request->phone;
    if($record->save()){
        return redirect("my")->with("success","Record Successfully Updated");
    }else{
        return redirect("my")->with("error","Something went wrong");
    }
}

Upvotes: 1

Views: 97

Answers (2)

Md. Amirozzaman
Md. Amirozzaman

Reputation: 1125

For update request,Your method should be PUT; so

<form method="POST">
  @csrf
  @method('PUT')
  <!---whatever your code -->
</form>

or

{{ Form::open(array('url' => '/', 'method' => 'PUT')) }}
    .... wathever code here
{{ Form::close() }}

Note: Since HTML forms only support POST and GET, PUT and DELETE methods will be spoofed by automatically adding a _method hidden field to your form.

Read this doc

Upvotes: 1

xenex-media
xenex-media

Reputation: 183

1.First of all it is a post method add method in the form header and you are using resource in the routes so you need to specify the patch method in your form 2.You have to pass csrf_token for post method on the top of the form you can do @csrf Like this

<form method="POST">
    @csrf
    @method("METHOD")
</form>

You are pssing id from your form so In controller you can do simple like this

3.Check the routes/web.php if u have done post

public function update(Request $request,Record $record){
    $record->name=$request->name;
    $record->email=$request->email;
    $record->phone=$request->phone;
    $record->save();

    return redirect()->to('/url');
}

Also make sure you have pass the id to the routes Hope this helps Good luck

Upvotes: 1

Related Questions