Laravel 9 Livewire Form Submit Example

Hi Dev,

This simple article demonstrates of laravel 9 livewire form submit example. In this article, we will implement a create form with laravel 9 livewire. We will use laravel 9 livewire tutorial. This article will give you simple example of install livewire to laravel 9.

Livewire is a full-stack framework for Laravel framework that makes building dynamic interfaces simple, without leaving the comfort of Laravel. if you are using livewire with laravel then you don’t worry about writing jquery ajax code, livewire will help to write very simple way jquery ajax code using PHP. without page refresh laravel validation will work, a form will submit, etc.

Here, I will give you a very simple example of creating a contact form with a name and email and I will store that data in the database without refreshing the page and too many lines of code in the blade file. we will use only livewire/livewire package.

Laravel 9 Livewire Form Submit Example

Step 1: Install Laravel

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create Migration and Model

Here, we need create database migration for contacts table and also we will create model for contacts table.

php artisan make:migration create_contacts_table

Migration:

<?php

use IlluminateDatabaseMigrationsMigration;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateSupportFacadesSchema;

return new class extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('contacts', function (Blueprint $table) {

$table->id();

$table->string('name');

$table->string('email');

$table->text('body');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('products');

}

};

php artisan migrate

now we will create Contact model by using following command:

php artisan make:model Contact

App/Models/Contact.php

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;

use IlluminateDatabaseEloquentModel;

class Contact extends Model

{

use HasFactory;

/**

* Write code on Method

*

* @return response()

*/

protected $fillable = [

'name', 'email', 'body'

];

}

Step 3: Install Livewire

now in this step, we will simply install livewire to our laravel application using bellow command:

composer require livewire/livewire

Step 4: Create Component

Now here we will create livewire component using their command. so run bellow command to create contact us form component.

php artisan make:livewire contact-form

Now they created fies on both path:

app/Http/Livewire/ContactForm.php

resources/views/livewire/contact-form.blade.php

Now both file we will update as bellow for our contact us form.

app/Http/Livewire/ContactForm.php

<?php

namespace AppHttpLivewire;

use LivewireComponent;

use AppModelsContact;

class ContactForm extends Component

{

public $name;

public $email;

public $body;

public function submit()

{

$validatedData = $this->validate([

'name' => 'required|min:6',

'email' => 'required|email',

'body' => 'required',

]);

Contact::create($validatedData);

return redirect()->to('/form');

}

public function render()

{

return view('livewire.contact-form');

}

}

resources/views/livewire/contact-form.blade.php

<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">Body</label>

<textarea class="form-control" id="exampleInputbody" placeholder="Enter Body" wire:model="body"></textarea>

@error('body') <span class="text-danger">{{ $message }}</span> @enderror

</div>

<button type="submit" class="btn btn-primary">Save Contact</button>

</form>

Step 5: Create Route

now we will create one route for calling our example, so let’s add new route to web.php file as bellow:

routes/web.php

Route::get('/form', function () {

return view('form');

});

Step 6: Create View File

here, we will create blade file for call form route. in this file we will use @livewireStyles, @livewireScripts and @livewire(‘contact-form’). so let’s add it.

resources/views/form.blade.php

<!DOCTYPE html>

<html>

<head>

<title></title>

@livewireStyles

<link rel="stylesheet" href="{{ asset('css/app.css') }}">

</head>

<body>

<div class="container">

<div class="card">

<div class="card-header">

Laravel Livewire Example - ItSolutionStuff.com

</div>

<div class="card-body">

@livewire('contact-form')

</div>

</div>

</div>

</body>

<script src="{{ asset('js/app.js') }}"></script>

@livewireScripts

</html>

Now you can run using bellow command:

php artisan serve

Open bellow URL:

http://localhost:8000/form

Output:

Laravel 9 Livewire Form Submit Example

I hope it can help you…

Leave a Comment