Laravel 9 Livewire CRUD Example with Modal

Hello Artisan,

In this Laravel Livewire 9 crud tutorial, I will show you step by step that how we can create Laravel 9 Livewire crud application. So this Laravel 9 Livewire crud example will give you a step by step guide to build an application like spa.

So from this example, We will learn laravel 9 livewire crud with jetstream and tailwind css for design. I have explained simple step by step laravel 9 livewire crud example with a modal and tailwind css.

You know that Livewire is a full-stack frontend framework for Laravel that makes building dynamic interfaces simple, without leaving the comfort of Laravel. It is a frontend framework. After learning it you will just say Say hello to Livewire. 

You can also check the git repository of this project. if you face any error, please have look on it and check you code.

 

Git Repository : Laravel Livewire Crud

 

Table of Contents 

  • Download Laravel App
  • Create Routes
  • Create Model and Migration
  • Install Livewire
  • Create Livewire Component and View
  • Create Blade File
  • Run Project

 

You can check the project preview also from this below image. Just have a look.

 

Preview | User List

laravel-9-livewire-crud-example-tutorial

 

Preview | User Edit Form

laravel-9-livewire-crud-edit-example

 

Step 1 : Download Laravel 9

In this laravel 9 livewire crud tutorial i will explain it from step by step. So download the latest laravel app by the following command.

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

 

Step 2 : Create Routes

Now we need to create our routes. So paste this below code in web.php file.

routes/web.php

Route::view('contacts', 'users.contacts');

Here we used view route and users.contacts indicates blade view directories. So make it like below image.

laravel-livewire-example

Now paste this following code to contacts.blade.php.

resources/views/users/contacts.blade.php

 

Step 3 : Create Model and Migration

Now we have to create our model and migration to submit form data in database so that we can check laravel livewire example crud tutorial. So make it by below command.

php artisan make:model Contact -m

 

Now open Contact model and paste below code.

app/Contact.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Contact extends Model
{
    protected $guarded = [];
}

 

now add this two field to your contacts table and run migrate command to save it database.

database/migrations/create_contacts_table.php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateContactsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('contacts', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('contacts');
    }
}

 

And then run

php artisan migrate

 

Step 4 : Install Livewire

To setup livewire in your project, run below command

composer require calebporzio/livewire

 

After running this command open your app.blade.php and paste this below code.

resources/views/layouts/app.blade.php

 

Step 5 : Create Livewire Component and View

Run the below command to create livewire view and component.

php artisan make:livewire contact

 

After running this command you will find two file in the following path app/Http/Livewire/Contact.php and resources/views/livewire/contact.blade.php

app/Http/Livewire/Contact.php

namespace App\Http\Livewire;
use App\Contact;
use Livewire\Component;
class Contact extends Component
{
    public $data, $name, $email, $selected_id;
    public $updateMode = false;

    public function render()
    {
        $this->data = Contact::all();
        return view('livewire.contact');
    }
    private function resetInput()
    {
        $this->name = null;
        $this->email = null;
    }
    public function store()
    {
        $this->validate([
            'name' => 'required|min:5',
            'email' => 'required|email:rfc,dns'
        ]);
        Contact::create([
            'name' => $this->name,
            'email' => $this->email
        ]);
        $this->resetInput();
    }
    public function edit($id)
    {
        $record = Contact::findOrFail($id);
        $this->selected_id = $id;
        $this->name = $record->name;
        $this->email = $record->email;
        $this->updateMode = true;
    }
    public function update()
    {
        $this->validate([
            'selected_id' => 'required|numeric',
            'name' => 'required|min:5',
            'email' => 'required|email:rfc,dns'
        ]);
        if ($this->selected_id) {
            $record = Contact::find($this->selected_id);
            $record->update([
                'name' => $this->name,
                'email' => $this->email
            ]);
            $this->resetInput();
            $this->updateMode = false;
        }
    }
    public function destroy($id)
    {
        if ($id) {
            $record = Contact::where('id', $id);
            $record->delete();
        }
    }
}

 

Step 6 : Create Blade File

Now create a folder structure like below image. We are in the last past of our livewire tutorial.

laravel-livewire-crud

 

Now paste this below code to you blade file. Here we will create contact, create and update file to create our laravel livewire crud example.

resources/views/livewire/contact.blade.php

 

And now update create.blade.php like below:

resources/views/livewire/create.blade.php

 

resources/views/livewire/update.blade.php

 

Now all are set to go. Just visit this below url to test our laravel livewire crud example project.

http://127.0.0.1:8000/contacts

 

Read also : Laravel Livewire - Run Your PHP Code Like JavaScipt

 

Hope this Laravel 9 Livewire crud example tutorial will help you.

 

author-image
Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js