Laravel 9 Form Collective Example

Websolutionstuff | Jan-20-2023 | Categories : Laravel

In this article, we will see laravel 9 form collective example. Here, we will learn how to use collective form and how to install form in laravel 8 and laravel 9. Sometimes you got an error like class "form" not found in laravel 7, laravel 8, and laravel 9. So, we will solve class not found in laravel 9.

So, let's see the laravel 9 form example, laravelcollective/html, class form not found in laravel 9, class form not found in laravel 9, and how to install form collective in laravel 9.

laravel 9 Form

 

laravelcollective/html is provided an HTML textbox, radio button, select box, checkbox, and many more with laravel. They provide different methods to use those input fields we need to add this facade class 'collective\html\formfacade' if not added.

 

Install Laravel Collective Form

In this step, we will install the laravel collective form using the composer command.

$ composer require laravelcollective/html

Note: If you are using the older version of laravel then you need to add the below code in the app.php file.

'providers' => [
    ...
    Collective\Html\HtmlServiceProvider::class,
    ...
  ],
'aliases' => [
      ...
      'Form' => Collective\Html\FormFacade::class,
      'Html' => Collective\Html\HtmlFacade::class,
      ...
  ],

 

 

Use of Form

Now, we will see how to use the laravel collective form and different input types.

Opening A Form

{!! Form::open(['url' => 'form/example']) !!}
    //
{!! Form::close() !!}

By default, a POST method will be assumed. But, you are free to specify any other method like the below code.

Form::open(['url' => 'foo/bar', 'method' => 'put'])

Note: HTML forms only support POST and GETPUT and DELETE methods.

 

Use Route and Controller in Form

Now, we will how to use the route in form and how to use the controller with its method in form.

Form::open(['route' => 'route.name'])

Form::open(['action' => 'UserController@index'])

You can pass parameters in the form like the below code.

Form::open(['route' => ['user.edit', $user->id]])

Form::open(['action' => ['UserController@edit', $user->id]])

 

Use Label with HTML Attribute

Now, we will see the form label with HTML attributes like palace holder and class name.

Form::label('First Name', 'Enter First Name', ['class' => 'awesome']);

Form::label('Last Name', 'Enter Last Name', ['class' => 'awesome']);

Note: After creating a label, any form element you create with a name matching the label name will automatically receive an ID matching the label name as well.

 

Use HTML Input Type

Laravel collective form provides HTML input types like text, email, password, file, checkbox, radio button,s and much more.

// Input type Text
Form::text('email', '[email protected]');

// Input type Password
Form::password('password', ['class' => 'awesome']);

// Input type Checkbox
Form::checkbox('name', 'value'); 
or 
Form::checkbox('name', 'value', true);


// Input type Radio Button 
Form::radio('name', 'value');
or
Form::radio('name', 'value', true);

// Input type Number
Form::number('name', 'value');

// Input type Date
Form::date('name', \Carbon\Carbon::now());

// Input type File
Form::file('image');

// Input type Dropdown
Form::select('size', ['L' => 'Large', 'S' => 'Small']);
or
Form::select('size', ['L' => 'Large', 'S' => 'Small'], 'S');

// Input type Submit
Form::submit('Submit!');

 


You might also like:

Recommended Post
Featured Post
How To Fix MySQL Shutdown Unexpectedly In XAMPP
How To Fix MySQL Shutdown Unex...

In this article, we will see how to fix "Error: MySQL shutdown unexpectedly" in XAMPP. When I open XAMPP...

Read More

Sep-22-2022

How to Create Custom Error Page in Laravel 11
How to Create Custom Error Pag...

Hello developers! In this article, we'll see how to create a custom error page in laravel 11. Here, we'll custom...

Read More

May-08-2024

Datatables Localization Example
Datatables Localization Exampl...

In this post i will show you datatables localization example, jquery datatable plugin can localize a...

Read More

May-10-2021

How To Disable Specific Dates In jQuery Datepicker
How To Disable Specific Dates...

In this tutorial, we will see how to disable specific dates in jquery datepicker. The jQuery UI Datepicker is...

Read More

Jun-16-2022