Laravel 7.x Livewire CRUD Tutorial

Hello Artisan

Today i am coming with a brand new tutorial on the topic of laravel livewire. A couple of days ago i created a tutorial on the topic of how laravel livewire works. But in this i will show you a depth concept of laravel livewire.

In this brand new tutorial i will discuss about laravel livewire crud example from scratch. In this laravel livewire examples tutorial you will learn how to create crud operation in laravel using livewire. In this laravel livewire tutorial you will learn it from step by step.

Livewire is a full-stack 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". 

I will do this livewire crud tutorial using the version of laravel 7. After finishing this project you will see that it will work like javascript framework vue js or javascript libraary react js. Using laravel livewire you can insert, update or delete data without page refresh. How amazing it is. Isn't it? So let's start our laravel livewire crud example tutorial.

 

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-livewire-crud-example-tutorial

Preview | User Edit Form

laravel-livewire-edit-example

Step 1 : Download Laravel 7

In this laravel 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

 

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

Now if you checked it then i think you must say that if vue and blade had a baby it would be a jellyfish. Isn't it? Hope it can help you.