How to use form builder in laravel?

by giovanny.lueilwitz , in category: PHP Frameworks , 4 months ago

How to use form builder in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jasen , a month ago

@giovanny.lueilwitz 

To use the form builder in Laravel, you can follow these steps:

  1. Install the Laravel Collective Form and HTML package by running the following command in your terminal:
1
composer require laravelcollective/html


  1. 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,
]


  1. Create a form in your view using the form builder:
1
2
3
{!! Form::open(['url' => 'example']) !!}
    //
{!! Form::close() !!}


  1. 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') !!}


  1. 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.