0

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">

Hamad_1995
  • 11
  • 2
  • 1
    share your routing – Shibon Jun 04 '20 at 10:26
  • 1
    php artisan route:list – Collin Jun 04 '20 at 10:29
  • You need to define the route as `Route::put('posts/{id}', ...)` to get it to respond to `PUT` requests – apokryfos Jun 04 '20 at 10:34
  • i am using the same update route – Hamad_1995 Jun 04 '20 at 10:35
  • Does this answer your question? [Laravel form html with PUT method for PUT routes](https://stackoverflow.com/questions/28143674/laravel-form-html-with-put-method-for-put-routes) – Shibon Jun 04 '20 at 10:37
  • please copy the route you use and paste it in your question – OMR Jun 04 '20 at 10:41
  • 1
    If you are using a resource controller routing you should use `action="{{ route('posts.update', [ 'post' => $posts->id ])) }}"` to be extra sure you're getting the correct route – apokryfos Jun 04 '20 at 10:42
  • edit your question & post your route – STA Jun 04 '20 at 11:22
  • Is there any good reason to write a `form` tag containing `method=POST` and defining another method one line below? – Nico Haase Jun 04 '20 at 12:16
  • @NicoHaase HTML forms don't support methods other than GET and POST and therefore frameworks like Laravel have to resort to these sort of hacks to "simulate" other methods – apokryfos Jun 06 '20 at 13:50
  • @apokryfos what do you mean by that? What **exactly** is simulated there, and how? – Nico Haase Jun 06 '20 at 14:59
  • @NicoHaase check https://stackoverflow.com/a/8054241/487813 for more information. The actual override in code happens [here](https://github.com/symfony/http-foundation/blob/master/Request.php#L1249) in the base symfony request (which Laravel extends) – apokryfos Jun 06 '20 at 17:48

4 Answers4

1

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}', ...)
A.A Noman
  • 5,244
  • 9
  • 24
  • 46
0

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

Digvijay
  • 7,836
  • 3
  • 32
  • 53
0

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');
STA
  • 30,729
  • 8
  • 45
  • 59
Collin
  • 914
  • 1
  • 9
  • 30
0

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>
Hamad_1995
  • 11
  • 2