Home » Php » Laravel form html with PUT method for PUT routes

Laravel form html with PUT method for PUT routes

Posted by: admin October 29, 2017 Leave a comment

Questions:

I Have this in my routes :

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

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

Answers:

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>     

Questions:
Answers:

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

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

Questions:
Answers:

in your view blade change to

{{ Form::open(['action' => '[email protected]', 'method' => 'PUT', 'class' = 'your class here']) }}

<div>
{{ Form::textarea('textareanamehere', 'default value here', ['placeholder' => 'your place holder here', 'class' => 'your class here']) }}
</div>

<div>
{{ Form::submit('Update', ['class' => 'btn class here'])}}
</div>

{{ Form::close() }}

actually you can use raw form like your question. but i dont recomended it. dan itulah salah satu alasan agan belajar framework, simple, dan cepat. so kenapa pake raw form kalo ada yang lebih mudah. hehe. proud to be indonesian.

reference (laravel blade form)[http://laravel-recipes.com/recipes/124/opening-a-new-html-form]