Hamad_1995
Hamad_1995

Reputation: 11

put method not supported in laravel

I tried to update a post using edit route but when I send the form and use the update function give me an error enter image description here

my code is

<form action="/posts{{$posts->id}}" method="POST">
@method('PUT')
@csrf
<label for="">title</label>
<input type="text" name="title" class="form-control" >
<label for="">body</label>
<textarea type="text" name="body" class="form-control">{{$post->body}}</textarea>
<input type="submit" class="btn btn-primary" value="edit">

Upvotes: 0

Views: 197

Answers (4)

Hamad_1995
Hamad_1995

Reputation: 11

I put a hidden method that I found on laravel documents and worked fine

<form action="/posts/{{$post->id}}" method="POST">
@csrf
<label for="">title</label>
<input type="text" name="title" class="form-control" >
<label for="">body</label>
<textarea type="text" name="body" class="form-control">{{$post->body}}. 
</textarea>
<input type="submit" class="btn btn-primary" value="edit">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

Upvotes: 0

Collin
Collin

Reputation: 944

You could do the following :

<form action="{{ route('route.name', $post->id) }}" method="POST">
@csrf
<label for="">title</label>
<input type="text" name="title" class="form-control" >
<label for="">body</label>
<textarea type="text" name="body" class="form-control">{{$post->body}}</textarea>
<input type="submit" class="btn btn-primary" value="edit">

And for the route :

Route::post('/posts/{id}', 'Controller@function')->name('route.name');

Upvotes: 0

A.A Noman
A.A Noman

Reputation: 5270

You have to use like this

<form action="{{url('')}}/posts/{{$post->id}}" method="POST">
@csrf
<label for="">title</label>
<input type="text" name="title" class="form-control" >
<label for="">body</label>
<textarea type="text" name="body" class="form-control">{{$post->body}}</textarea>
<input type="submit" class="btn btn-primary" value="edit">

And in your route use like this

Route::post('/posts/{id}', ...)

Upvotes: 1

Digvijay
Digvijay

Reputation: 8927

You are missing a / in your action action="/posts/{{ $posts->id }}"

Upvotes: 0

Related Questions