We can use normal HTML forms or collective forms to submit our form data in the Laravel application. If we use normal HTML form then there is no need to install any packages. But if you use the collective form in Laravel then you have to install the collective form package before using it.

In this Laravel collective form Laravel 9 tutorial, I will show you how to use the collective form in Laravel 9 application. I will create a user form and then I will show you also collective form validation. First, we will install the collective form package then we will create a collective form and then we will submit it and then we will return data from the Laravel controller.

Let's see the preview of the Laravel collective form example:

laravel-collective-form-example

Step 1: Download Fresh Laravel

In this first step, we need a fresh Laravel 9 application. So download it by the below command:

composer create-project laravel/laravel example-app

 

Step 2: Install Collective Form

We have to install the package in this step, so run the below command:

composer require laravelcollective/html

 

Step 3:  Create Route

Now in this, create a route like the one below to complete Laravel collective form example:

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

Route::get('user/create', [UserController::class, 'create'])->name('user.create');
Route::post('user/create', [UserController::class, 'store'])->name('user.store');

 

Read also: Laravel 9 Form Submit Example

 

Step 4: Create Controller

Now in this step, we have to create a 'UserController' to complete Laravel collective form Laravel 9. So create a controller by the below command:

php artisan make:controller UserController

 

Now update the controller like the below:

app/Http/Controllers/UserCountroller.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{

    public function create()
    {
        return view('user.create');
    }

    public function store(Request $request)
    {   
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:6',
        ]);

        return $request->all();
    }
}

 

Step 5: Create Views

In this step, we have to create our views for the collective form to create users. So let's create the form.

resources/views/user/create.blade.php

@extends('layouts.app')

@push('style')
@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header" style="background: gray; color:#f1f7fa; font-weight:bold;">
                    Create New User
                    <a href="{{ route('user.index') }}" class="btn btn-success btn-xs py-0 float-end">Back</a>
                </div>
                @if(session('message'))
                    <div class="alert alert-{{ session('status') }} alert-dismissible fade show mt-3" role="alert">
                        <strong>{{ session('message') }}</strong>
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                @endif
                 <div class="card-body">                    
                    {!! Form::open(['route' => 'user.store', "method" => "post", "class" => "form", "id" => "createUserForm"]) !!}
                        {!! Form::token() !!}
                        <div class="form-group mt-3">
                            {!! Form::label("name", "Name") !!}
                            {!! Form::text("name", old('name'), ['class' => 'form-control','placeholder' => 'Name']) !!}
                            {!! $errors->first("name",'<span class="help-block">:message</span>') !!}
                        </div>
                        <div class="form-group mt-3">
                            {!! Form::label("email", "Email") !!}
                            {!! Form::text("email", old('email'), ['class' => 'form-control','placeholder' => 'Email']) !!}
                            {!! $errors->first("email",'<span class="help-block">:message</span>') !!}
                        </div>
                        <div class="form-group mt-3 mb-3">
                            {!! Form::label("password", "Password") !!}
                            {!! Form::password("password", old('password'), ['class' => 'form-control','placeholder' => 'Password']) !!}
                            {!! $errors->first("password",'<span class="help-block">:message</span>') !!}
                        </div>
                        {!! Form::submit("Submit", ["class"=>"btn btn-primary"]) !!}
                    {!! Form::close() !!}
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

 

Read also: Laravel 9 CRUD Tutorial Using Query Builder

 

Now all are set to go. Now run php artisan serve command to start the development server and visit the following URL to check this Laravel collective form example.

URL
http://localhost:8000/user/create

 

Now if you submit this form with data then you will see the following output:

{
   "_token": "tjSyvgVM85Whd9AY3GtGDZDNIiBryDao5wrCrjcW",
   "name": "Mahedi Hasan",
   "email": "mahedy15@gmail.com",
   "password": "123456"
}

 

Conclusion

Hope this laravel collective form laravel 9 tutorial will help. After completing this laravel collective form install tutorial, your concept will clear about how to use the collective form in laravel. Hope this laravel collective form example tutorial will clarify your concept about laravel collective form.