Laravel 5.4 - Updating a resource Laravel 5.4 - Updating a resource laravel laravel

Laravel 5.4 - Updating a resource


you should use

{{ method_field('PATCH') }}

as your form field

and change action to

/posts/{{ $post->id }}

like this:

<form method="POST" action="/posts/{{ $post->id }}">        {{ csrf_field() }}{{ method_field('PATCH') }}        <div class="form-group">            <label for="title">Title</label>            <input name="title" type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" value="{{ $post->title }}" required>        </div>        <div class="form-group">            <label for="description">Description</label>            <input name="description" type="text" class="form-control" id="exampleInputPassword1" value="{{ $post->title }}" required>        </div>        <div class="form-group">            <button type="submit" class="btn btn-primary">Update</button>        </div>    </form>


There are a couple of things you have missed out.

Firstly, as @Maraboc pointed out in the comments you need to add method spoofing as standard HTML forms only allow for GET and POST methods:

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

or

{{ method_field('PATCH') }}

https://laravel.com/docs/5.4/routing#form-method-spoofing

Then you will also need to omit the "edit" uri in your forms action:

<form method="POST" action="/posts/{{ $post->id }}">

or

<form method="POST" action="{{ url('posts/' . $post->id) }}">

https://laravel.com/docs/5.4/controllers#resource-controllers

(scroll down a little bit to the Actions Handled By Resource Controller section)

You also may find it helpful to watch https://laracasts.com/series/laravel-5-from-scratch/episodes/10

Hope this helps!


When building an API, you may need a transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application's users. Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON.