Home > PHP Framework > Laravel > body text

Handle Form forms gracefully in Laravel

藏色散人
Release: 2019-10-23 22:59:55
forward
5079 people have browsed it

When developing Laravel applications, it usually involves form processing. Regarding how to elegantly process and reuse Laravel's Form forms, the following is a small life experience:

Use Form Package

Although writing native HTML code is indeed more readable, in fact Form Package will still bring us a lot of convenience, such as using Form::model() and Form::select().

Imagine an example: we need to add or update a user's username

We can design a code and directory structure similar to this:

// 位于 resources/views/users/edit.blade.php
{!! Form::model($user, ['route' => ['users.update', $user->id], 'method' => 'put']) !!}
    @include('users._form')
    // Your cancel / update buttons
{!! Form::close() !!}
// 位于 resources/views/users/_form.blade.php
<div class="form-group">
    {!! Form::label(&#39;name&#39;, &#39;Name&#39;) !!}
    {!! Form::text(&#39;name&#39;) !!}
</div>
Copy after login

The HTML generated by the above Form The code probably looks like this:

<form method="POST" action="http://example.com/users/1" accept-charset="UTF-8">
    <input name="_token" type="hidden" value="Q5oILhAr92pVqfE0ZSSXjSdJuUi09DVSPWweHQlq">
    <input name="_method" type="hidden" value="PUT">
    <div class="form-group"> 
        <label for="name">Name</label>
        <input name="name" type="text" value="Michael">
    </div>
</form>
Copy after login

Note that in the Form::model() method, we pass the $user variable, which means: if $user has a value of the name attribute, the form will This value is automatically filled in without us having to write it manually.

The second point is that since we are passing $user to Form::model(), we can use this little trick in the controller:

class UserController extends Controller
{
    public function create()
    {
        return view(&#39;users.create&#39;, [&#39;user&#39; => new User]);
    }
    public function edit(User $user)
    {
        return view(&#39;users.edit&#39;, [&#39;user&#39; => $user]);
    }
}
Copy after login

In this case, you There will be no conflict when editing and adding. There is a name value when editing and updating, but there is no need for a name value when adding. So we can write the entire Form like this:

// 位于 resources/views/users/_form.blade.php
<div class="form-group">
    <label for="name">Name</label>
    <input type="text" name="name" value="{{ old(&#39;name&#39;, $user->name) }}" class="form-control">
</div>
Copy after login

And you’re done!

Finally

It’s still the same sentence: You can still consider Form Package when processing forms, especially when you are processing Form Package.

For more Laravel related technical articles, please visit the Laravel Framework Getting Started Tutorial column to learn!

The above is the detailed content of Handle Form forms gracefully in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:codecasts
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template