In this tutorial, we are going to learn how to implement Multi Step Form in Laravel. When you need to add something without single step you can use this method to add it through multiple steps.
Prerequisites
Laravel
To Continue with this tutorial, you should have installed Laravel 8 in your pc. If you are still not installed Laravel in your machine you can configure it from here.
Setting up the Project
First you need to create a new laravel project by running below command in your terminal
Then navigate to your project directory by using below command in your terminal
1
cd laravel-multistep
Setting up Database
To setup up Database for our project, open the application using your favourite text editor and and then navigate to .env file in it and then change below section acording to your Database settings:
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateProductsTable extends Migration { public function up() { Schema::create('products', function (Blueprint $table) { $table->id(); $table->string('name')->nullable(); $table->longText('description')->nullable(); $table->float('amount')->nullable(); $table->boolean('status')->default(0); $table->integer('stock')->default(0); $table->timestamps(); }); } public function down() { Schema::dropIfExists('products'); } }
Then run below command in your terminal to migrate our newly created product migration file:
1
php artisan migrate
Create Models
Now we need to create Product model. To create Product model run below command in your terminal:
1
php artisan make:model Product
Then navigate to Product model and paste below code on it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Product extends Model { protected $fillable = [ 'name', 'amount', 'description', 'status', 'stock' ]; }
Create Product Controller
Now we need to create contoller to handle our core logic for handling our Datata and views. To create new controller:
1
php artisan make:controller ProductController
After that open your app/Http/Controllers/ProductController.php and paste below code in it:
class ProductController extends Controller { public function index() { $products = Product::all(); return view('products.index',compact('products')); }
public function createStepOne(Request $request) { $product = $request->session()->get('product'); return view('products.create-step-one',compact('product')); } public function postCreateStepOne(Request $request) { $validatedData = $request->validate([ 'name' => 'required|unique:products', 'amount' => 'required|numeric', 'description' => 'required', ]); if(empty($request->session()->get('product'))){ $product = new Product(); $product->fill($validatedData); $request->session()->put('product', $product); }else{ $product = $request->session()->get('product'); $product->fill($validatedData); $request->session()->put('product', $product); } return redirect()->route('products.create.step.two'); } public function createStepTwo(Request $request) { $product = $request->session()->get('product'); return view('products.create-step-two',compact('product')); } public function postCreateStepTwo(Request $request) { $validatedData = $request->validate([ 'stock' => 'required', 'status' => 'required', ]); $product = $request->session()->get('product'); $product->fill($validatedData); $request->session()->put('product', $product); return redirect()->route('products.create.step.three'); } public function createStepThree(Request $request) { $product = $request->session()->get('product'); return view('products.create-step-three',compact('product')); } public function postCreateStepThree(Request $request) { $product = $request->session()->get('product'); $product->save(); $request->session()->forget('product'); return redirect()->route('products.index'); } }
Create Views
Inside resources -> views folder create, folder called layouts. Then create an app.blade.php file inside i and add the following code.
In this tutorial, we implemented Multistep Form in Laravel. If you have any issue regarding this tutorial, mention your issue in comment section or reach me through my E-mail. You can obtain complete source code and asset files for this tutorial from this GitHub repository.