@giovanny.lueilwitz
To use the form builder in Laravel, you can follow these steps:
- Install the Laravel Collective Form and HTML package by running the following command in your terminal:
1
|
composer require laravelcollective/html
|
- Add the service provider and facade to your app/config/app.php file:
1
2
3
4
5
6
7
8
9
10
|
'providers' => [
// ...
CollectiveHtmlHtmlServiceProvider::class,
],
'aliases' => [
// ...
'Form' => CollectiveHtmlFormFacade::class,
'Html' => CollectiveHtmlHtmlFacade::class,
]
|
- Create a form in your view using the form builder:
1
2
3
|
{!! Form::open(['url' => 'example']) !!}
//
{!! Form::close() !!}
|
- Add form fields using the form builder methods:
1
2
3
4
5
6
7
|
{!! Form::label('email', 'Email Address') !!}
{!! Form::text('email') !!}
{!! Form::label('password', 'Password') !!}
{!! Form::password('password') !!}
{!! Form::submit('Submit') !!}
|
- Handle the form submission in your controller:
1
2
3
4
5
6
7
8
9
10
11
|
public function store(Request $request)
{
// Validate the form data
$validatedData = $request->validate([
'email' => 'required|email',
'password' => 'required',
]);
// Process the form data
// ...
}
|
That's it! You have now successfully used the form builder in Laravel.