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
Carbon Add Minutes In Laravel
Carbon Add Minutes In Laravel

In this example, we will see carbon add minutes in laravel. Here we will give you a simple example of laravel carbo...

Read More

Dec-11-2020

Laravel 9 Create Middleware For XSS Protection
Laravel 9 Create Middleware Fo...

In this article, we will see laravel 9 create middleware for XSS protection. Cross-site scripting is a type of...

Read More

Apr-30-2022

How to Send Bulk Mail Using Queue in Laravel 8
How to Send Bulk Mail Using Qu...

In this article, we will see how to send bulk mail using a queue in laravel 8. Laravel queue is used for sending bu...

Read More

Feb-10-2021

Mastering Reactive Forms Validation in Angular 15
Mastering Reactive Forms Valid...

Welcome to "Mastering Reactive Forms Validation in Angular 15: A Comprehensive Guide." In this guide, we will...

Read More

Jun-14-2023