Problem
You want to start a form.
You know you could use the HTML <form>
tag directly, but want to use Laravel’s Form facade.
Solution
Use the Form::open()
method.
Usually, this is done in a Blade template. There are several ways to use this method.
Using defaults
The HTML produced is.
<input name="_token" type="hidden" value="somelongrandom string">
This starts a form, using the POST method, to the current URL and will add an accept-charset=”UTF-8″ to the form. Additionally, a hidden token is added.
To a specific url
Instead of passing an action you should pass a url value. This occurs in the only argument Form::open() accepts … an array.
This produces the following HTML.
<input name="_token" type="hidden" value="somelongrandom string">
To a route
Instead of passing the action you should pass a route value to one of your named routes.
If the route doesn’t exist an error will be produced. Otherwise the form’s action attribute becomes the full URL to the route.
<input name="_token" type="hidden" value="somelongrandom string">
To a controller action
This is where you use action.
If the controller or method doesn’t exist an error will be produced. Otherwise the form’s action attribute becomes the full URL to the route that will call the specified controller and method.
<input name="_token" type="hidden" value="somelongrandom string">
Specifying different methods
You can use methods other than POST with your forms. Pass the ‘method’ you want in the array argument. Valid methods are ‘get’, ‘put’, ‘patch’, ‘post’, or ‘delete’.
This will produce the following HTML.
Notice there’s no token? The token is not added for GET methods.
See the discussion at the bottom of this recipe for how Laravel “fakes” the methods browsers can’t handle.
Specifying file uploads
If you pass a ‘files’ => true as one of the array arguments, the form will become suitable for file uploads.
The form now has the enctype=”multipart/form-data” attribute.
enctype="multipart/form-data">
<input name="_token" type="hidden" value="somelongrandom string">
Discussion
How Laravel “fakes” methods browsers cannot handle.
The form methods PUT, PATCH, and DELETE cannot be handled by most browsers. So what Laravel does is make the method=”POST” and adds a hidden field.
This produces the following.
<input name="_method" type="hidden" value="PUT">
<input name="_token" type="hidden" value="somelongrandom string">
The framework is smart enough to translate those hidden fields and change the request type to match what’s desired.
from Linux Hint https://ift.tt/3ln5qns
0 Comments