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 Install php-zip Extension In Ubuntu
How To Install php-zip Extensi...

In this article, I will guide you through the process of installing the php-zip extension on Ubuntu. The php-zip extensi...

Read More

Jul-14-2023

How To Count Days Between Two Dates In PHP Excluding Weekends
How To Count Days Between Two...

In this article, we will see how to count days between two dates in PHP excluding weekends. Here, we will learn to...

Read More

Jan-25-2023

How To Connect ftp Server Using php
How To Connect ftp Server Usin...

Hello All, In this post i will show you how to connect ftp server using php. PHP provide inbuilt functio...

Read More

May-12-2021

How to Get Soft Deleted Records in Laravel 10
How to Get Soft Deleted Record...

Hey there! Ever wondered how to recover deleted data in Laravel 10 without much hassle? Well, you're in luck! I'...

Read More

Nov-27-2023