Laravel form html with PUT method for PUT routes

137,206

Solution 1

You CAN add css clases, and any type of attributes you need to blade template, try this:

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

If you dont want to go the blade way you can add a hidden input. This is the form Laravel does, any way:

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. (Laravel docs)

<form class="col-md-12" action="<?php echo URL::to('/');?>/post/<?=$post->postID?>" method="POST">

    <!-- Rendered blade HTML form use this hidden. Dont forget to put the form method to POST -->

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

    <div class="form-group">
        <textarea type="text" class="form-control input-lg" placeholder="Text Here" name="post"><?=$post->post?></textarea>
    </div>

    <div class="form-group">
        <button class="btn btn-primary btn-lg btn-block" type="submit" value="Edit">Edit</button>
    </div>
</form>

Solution 2

If you are using HTML Form element instead Laravel Form Builder, you must place method_field between your form opening tag and closing end. By doing this you may explicitly define form method type.

<form>
{{ method_field('PUT') }}
</form>

For Laravel 5.1 and above

<form>
@method('PUT')
</form>

Solution 3

Just use like this somewhere inside the form

@method('PUT')

Solution 4

Is very easy, you just need to use method_field('PUT') like this:

Method 1:

<form action="{{ route('route_name') }}" method="post">
    {{ method_field('PUT') }}
    {{ csrf_field() }}
</form>

Method 2:

<form action="{{ route('route_name') }}" method="post">
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
  • Update: I added new method used with latest Laravel version

Method 3:

<form action="{{ route('route_name') }}" method="post">
    @method('PUT')
    @csrf
</form>

PUT method for PUT routes

Regards!

Solution 5

On Laravel 8 you can do:

<form action="{{ route('post.update', $post->id) }}" method="POST">
    @method('PUT')
    @csrf
</form>

Using the route helper method you can start using routes names which is simply more comfortable than using complete routes. That's why i do route('post.update', ...).

You will surely need the id of the resource you are going to edit hence $post->id in route(..., $post->id).

@method('PUT') and @csrf do exactly the same as in the answers above... it just looks better.

Share:
137,206
Richard Henokh Kurniawan
Author by

Richard Henokh Kurniawan

Updated on July 09, 2022

Comments

  • Richard Henokh Kurniawan
    Richard Henokh Kurniawan almost 2 years

    I Have this in my routes :

    +--------+---------------------------+--------------+---------------------------                                                                                                                ---------+----------------+---------------+
    | Domain | URI                       | Name         | Action                                                                                                                                             | Before Filters | After Filters |
    +--------+---------------------------+--------------+---------------------------                                                                                                                ---------+----------------+---------------+
    |        | GET|HEAD /                |              | postcontroller                                                                                                                                     | auth           |               |
    |        | GET|HEAD login            |              | homecontroller@dologin                                                                                                                             |                |               |
    |        | POST login                |              | homecontroller@dologin                                                                                                                             |                |               |
    |        | GET|HEAD logout           |              | homecontroller@dologout                                                                                                                            |                |               |
    |        | GET|HEAD post             | post.index   | postcontroller@index                                                                                                                               |                |               |
    |        | GET|HEAD post/create      | post.create  | postcontroller@create                                                                                                                              |                |               |
    |        | POST post                 | post.store   | postcontroller@store                                                                                                                               |                |               |
    |        | GET|HEAD post/{post}      | post.show    | postcontroller@show                                                                                                                                |                |               |
    |        | GET|HEAD post/{post}/edit | post.edit    | postcontroller@edit                                                                                                                                |                |               |
    |        | PUT post/{post}           | post.update  | postcontroller@update                                                                                                                              |                |               |
    |        | PATCH post/{post}         |              | postcontroller@update                                                                                                                              |                |               |
    |        | DELETE post/{post}        | post.destroy | postcontroller@destroy 
    

    Now, i want to make a form html that will use PUT method. Here it is my codes:

    <form class="col-md-12" action="<?php echo URL::to('/');?>/post/<?=$post->postID?>" method="put">
        <div class="form-group">
            <textarea type="text" class="form-control input-lg" placeholder="Text Here" name="post"><?=$post->post?></textarea>
        </div>
        <div class="form-group">
            <button class="btn btn-primary btn-lg btn-block" type="submit" value="Edit">Edit</button>
        </div>
    </form>     
    

    But i doesn't work to submit the form into post.edit.

    I Have googled and i got solution that i must use

    {{form:...etc
    

    But, i want the form still can done by CSS styling. Is there any solution guys? Thank You

  • Jay Bienvenu
    Jay Bienvenu over 7 years
    Neither of these methods works in v5.4. The Forms & HTML page linked above is empty when you change the version to 5.4,
  • Yevgeniy Afanasyev
    Yevgeniy Afanasyev over 5 years
    this is official for laravel 5.7
  • Cyrus V.
    Cyrus V. over 4 years
    you can use @method('PUT') for Laravel 5.7+
  • JamLizzy101
    JamLizzy101 about 2 years
    The @method field only really became available for Laravel 5.7. Anything under wouldn't work...