Laravel 8 Livewire Form Example Tutorial

Laravel 8 livewire form example. In this post, i will show you how to create and submit livewire form in laravel 8.

In this example post, i will install livewire package and create livewire form components using livewire artisan commands in laravel 8.

So, you will learn step by step how to create livewire form and submit livewire form in laravel 8.

Laravel 8 Livewire Form Example Tutorial

  • Step 1 – Install Laravel 8 Application
  • Step 2 – Database Configuration
  • Step 3 – Create Post Model & Migration
  • Step 4 – Create Routes
  • Step 5 – Installing Livewire Package
  • Step 6 – Build Livewire Components
  • Step 7 – Create Livewire Blade Views
  • Step 8 – Start Development Server
  • Step 9 – Run This App On Browser

Step 1 – Install Laravel 8 Application

In step 1, open your terminal and navigate to your local web server directory using the following command:

//for windows user
cd xampp/htdocs

//for ubuntu user
cd var/www/html

Then install laravel 8 latest application using the following command:

composer create-project --prefer-dist laravel/laravel Laravel8LivewireForm

Step 2 – Database Configuration

In step 2, open your downloaded laravel 8 app into any text editor. Then find .env file and configure database detail like following:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db name
DB_USERNAME=db user name
DB_PASSWORD=db password

Step 3 – Create Model & Migration

In step 3, open command prompt and navigate to your project by using the following command:

cd / Laravel8LivewireForm

Then create model and migration file by using the following command:

php artisan make:model Contact -m

The above command will create two files into your laravel 8 livewire form example application, which is located inside the following locations:

  • Laravel8LivewireForm/app/Models/Contact.php
  • Laravel8LivewireForm/database/migrations/create_contacts_table.php

Now, find Conatct.php model file inside Laravel8LivewireForm/app/Models directory. And open it then add the fillable property code into Contact.php file, like following:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    use HasFactory;

    protected $fillable = [
        'name', 'email', 'message'
    ];
}

Then, find create_contacts_table.php file inside Laravel8LivewireForm/database/migrations/ directory. Then open this file and add the following code into function up() on this file:

    public function up()
    {
        Schema::create('contacts', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email');
            $table->text('message');
            $table->timestamps();
        });
    }

Now, open again your terminal and type the following command on cmd to create tables into your selected database:

php artisan migrate

Step 4 – Create Routes

In step 4, open your web.php file, which is located inside routes directory. Then add the following routes into web.php file:

use App\Http\Livewire\Contactus;

Route::get('contact-us-form', Contactus::class);

Step 5 – Installing Livewire Package

In step 5, use the following command to install livewire package in laravel 8 app:

composer require livewire/livewire

Then install node js package:

npm install

Next run npm

npm run dev

Now, run database migration command:

php artisan migrate

Step 6 – Build Livewire Components

In step 6, use the following command to create livewire components in laravel 8 app:

php artisan make:livewire contactus

The above command will create two files, which is located on the following locations:

app/Http/Livewire/Contactus.php

resources/views/livewire/contactus.blade.php

So, open Contactus.php, which is located inside app/http/Livewire directory and add the following code into it:

<?php
  
namespace App\Http\Livewire;
  
use Livewire\Component;
use App\Models\Contact;
  
class Contactus extends Component
{
    public $name;
    public $email;
    public $body;
  
    public function submit()
    {
        $validatedData = $this->validate([
            'name' => 'required|min:6',
            'email' => 'required|email',
            'message' => 'required',
        ]);
  
        Contact::create($validatedData);
  
        return redirect()->to('/contact-us-form');
    }
  
    public function render()
    {
        return view('livewire.contactus');
    }
}

Next, open contactus.blade.php, which is located inside resources/views/livewire/ directory and add the following code into it:

<form wire:submit.prevent="submit">
    <div class="form-group">
        <label for="exampleInputName">Name</label>
        <input type="text" class="form-control" id="exampleInputName" placeholder="Enter name" wire:model="name">
        @error('name') <span class="text-danger">{{ $message }}</span> @enderror
    </div>
  
    <div class="form-group">
        <label for="exampleInputEmail">Email</label>
        <input type="text" class="form-control" id="exampleInputEmail" placeholder="Enter name" wire:model="email">
        @error('email') <span class="text-danger">{{ $message }}</span> @enderror
    </div>
  
    <div class="form-group">
        <label for="exampleInputbody">Message</label>
        <textarea class="form-control" id="exampleInputbody" placeholder="Enter Message" wire:model="message"></textarea>
        @error('message') <span class="text-danger">{{ $message }}</span> @enderror
    </div>
  
    <button type="submit" class="btn btn-primary">Save Contact</button>
</form>

Step 7 – Create Blade Views

In step 7, Navigate resources/views/livewire directory and create home.blade.php. Then add the following code into it:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel 8 Livewire Contact Form Tutorial From Scratch - laratutorials.com</title>

        <!-- Fonts -->
        <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">

        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.1/css/bootstrap.min.css">

        <!-- Styles -->
        <style>
            html, body {
                background-color: #fff;
                color: #636b6f;
                font-family: 'Nunito', sans-serif;
                font-weight: 200;
                height: 100vh;
                margin: 0;
            }

            .full-height {
                height: 100vh;
            }

            .flex-center {
                align-items: center;
                display: flex;
                justify-content: center;
            }

            .position-ref {
                position: relative;
            }

            .top-right {
                position: absolute;
                right: 10px;
                top: 18px;
            }

            .content {
                text-align: center;
            }

            .title {
                font-size: 84px;
            }

            .links > a {
                color: #636b6f;
                padding: 0 25px;
                font-size: 13px;
                font-weight: 600;
                letter-spacing: .1rem;
                text-decoration: none;
                text-transform: uppercase;
            }

            .m-b-md {
                margin-bottom: 30px;
            }
        </style>
    </head>
<body>
    <div class="container mt-5">
        <div class="row mt-5 justify-content-center">
            <div class="mt-5 col-md-8">
                <div class="card">
                  <div class="card-header bg-success">
                    <h2 class="text-white">Laravel 8 Livewire Form Tutorial From Scratch - laratutorials.com</h2>
                  </div>
                  <div class="card-body">
                    @livewire('contactus')
                  </div>
                </div>
            </div>
        </div>
    </div>
    @livewireScripts
</body>
</html>

Step 8 – Start Development Server

Finally, open your command prompt again and run the following command to start development server for your simple laravel 8 livewire form example app:

php artisan serve

Step 9 – Run This App On Browser

In step 9, open your browser and fire the following url into your browser:

http://127.0.0.1:8000/contact-us-form

Leave a Comment