In this tutorial you will learn about the Laravel 7 Livewire Crud Example and its application with practical example.
In this Laravel livewire crud example tutorial I will shoe you how to create simple crud application using livewire package in laravel. In this tutorial you will learn to create basic CRUD application using livewire package in laravel. In this article you will also learn how to validate laravel livewire forms, how to update livewire form data, and how to edit livewire form data in laravel.
Laravel 7 Livewire Crud Example
- Step 1: Download Laravel Fresh App
- Step 2: Setup Database Configuration
- Step 3: Install Livewire
- Step 4: Create Component
- Step 5: Add Route
- Step 6: Create Blade Views
- Step 7: Run Development Server
Step 1: Download Laravel Fresh App
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Setup Database Configuration
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name DB_USERNAME=Enter_Your_Database_Username DB_PASSWORD=Enter_Your_Database_Password |
Step 3: Install Livewire
In this step, we will install livewire Package via the composer dependency manager. Use the following command to install livewire Package.
1 |
composer require livewire/livewire |
Step 4: Create Component
In this step we will create a component using following command:
1 |
php artisan make:livewire users |
The above command will create two files on the following location:
1 2 |
app/Http/Livewire/Users.php resources/views/livewire/users.blade.php |
app/Http/Livewire/Users.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
<?php namespace App\Http\Livewire; use Livewire\Component; use App\User; class Users extends Component { public $users, $name, $email, $user_id; public $updateMode = false; public function render() { $this->users = User::all(); return view('livewire.users'); } private function resetInputFields(){ $this->name = ''; $this->email = ''; } public function store() { $validatedDate = $this->validate([ 'name' => 'required', 'email' => 'required|email', ]); User::create($validatedDate); session()->flash('message', 'Users Created Successfully.'); $this->resetInputFields(); } public function edit($id) { $this->updateMode = true; $user = User::where('id',$id)->first(); $this->user_id = $id; $this->name = $user->name; $this->email = $user->email; } public function cancel() { $this->updateMode = false; $this->resetInputFields(); } public function update() { $validatedDate = $this->validate([ 'name' => 'required', 'email' => 'required|email', ]); if ($this->user_id) { $user = User::find($this->user_id); $user->update([ 'name' => $this->name, 'email' => $this->email, ]); $this->updateMode = false; session()->flash('message', 'Users Updated Successfully.'); $this->resetInputFields(); } } public function delete($id) { if($id){ User::where('id',$id)->delete(); session()->flash('message', 'Users Deleted Successfully.'); } } } |
Step 5: Add Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 |
Route::view('users','livewire.home'); |
Step 6: Create Blade Views
In this step we will create following blade views file and update the following code into your files:
resources/views/livewire/users.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<div> @if($updateMode) @include('livewire.update') @else @include('livewire.create') @endif <table class="table table-bordered mt-5"> <thead> <tr> <th>No.</th> <th>Name</th> <th>Email</th> <th>Action</th> </tr> </thead> <tbody> @foreach($users as $value) <tr> <td>{{ $value->id }}</td> <td>{{ $value->name }}</td> <td>{{ $value->email }}</td> <td> <button wire:click="edit({{ $value->id }})" class="btn btn-primary btn-sm">Edit</button> <button wire:click="delete({{ $value->id }})" class="btn btn-danger btn-sm">Delete</button> </td> </tr> @endforeach </tbody> </table> </div> |
resources/views/livewire/home.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
<!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 Livewire CRUD Operation Tutorial From Scratch</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 justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header"> <h2>Laravel Livewire Crud - tutmake.com</h2> </div> <div class="card-body"> @if (session()->has('message')) <div class="alert alert-success"> {{ session('message') }} </div> @endif @livewire('users') </div> </div> </div> </div> </div> @livewireScripts </body> </html> |
resources/views/livewire/create.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<form> <div class="form-group"> <label for="exampleFormControlInput1">Name</label> <input type="text" class="form-control" id="exampleFormControlInput1" placeholder="Enter Name" wire:model="name"> @error('name') <span class="text-danger">{{ $message }}</span>@enderror </div> <div class="form-group"> <label for="exampleFormControlInput2">Email address</label> <input type="email" class="form-control" id="exampleFormControlInput2" wire:model="email" placeholder="Enter Email"> @error('email') <span class="text-danger">{{ $message }}</span>@enderror </div> <button wire:click.prevent="store()" class="btn btn-success">Save</button> </form> |
resources/views/livewire/update.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<form> <div class="form-group"> <input type="hidden" wire:model="user_id"> <label for="exampleFormControlInput1">Name</label> <input type="text" class="form-control" wire:model="name" id="exampleFormControlInput1" placeholder="Enter Name"> @error('name') <span class="text-danger">{{ $message }}</span>@enderror </div> <div class="form-group"> <label for="exampleFormControlInput2">Email address</label> <input type="email" class="form-control" wire:model="email" id="exampleFormControlInput2" placeholder="Enter Email"> @error('email') <span class="text-danger">{{ $message }}</span>@enderror </div> <button wire:click.prevent="update()" class="btn btn-dark">Update</button> <button wire:click.prevent="cancel()" class="btn btn-danger">Cancel</button> </form> |
Step 7: Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/users |