In this tutorial you will learn about the Laravel 8 Livewire JetStream CRUD Operations Tutorial and its application with practical example.
In this Laravel 8 Livewire JetStream CRUD Operations Tutorial I will show you how to create simple crud application using JetStream with livewire package in laravel. In this tutorial you will learn to create basic CRUD application using JetStream with 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 8 Livewire JetStream CRUD Operations Tutorial
In this step by step guide, we will be creating a simple student crud operation application with validation using JetStream with livewire package in laravel 8. In this example you will learn how to insert, read, update and delete data from database using JetStream with livewire package in laravel 8.
- Step 1: Set Up Laravel Project
- Step 2: Add Database Details in ENV
- Step 3: Create Model and Migration
- Step 4: Install Livewire and Jetstream
- Step 5: Create CRUD Components
- Step 6: Frame Up CRUD Component
- Step 7: Create Routes
- Step 8: Prepare Blade View
- Step 9: Start Development Server
Create Laravel Project
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel laravel_livewire_crud |
Setup Database Credentials
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=database_name DB_USERNAME=database_user_name DB_PASSWORD=database_password |
Create Model and Migration
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Student -m |
You need to add the $fillable array and add the table values such as name, email and mobile in the app/Models/Student.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Student extends Model { use HasFactory; protected $fillable = [ 'name', 'email', 'mobile' ]; } |
We also need to configure the migration table for students, so add the table properties in the database/migrations/create_students_table.php file.
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 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateStudentsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('students', function (Blueprint $table) { $table->id(); $table->string('name', 100); $table->string('email'); $table->string('mobile'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('students'); } } |
Install Livewire and Jetstream
In this step, we will install livewire and Jetstream Package via the composer dependency manager. Use the following command to install livewire and Jetstream Package.
1 |
composer require laravel/jetstream |
1 |
php artisan jetstream:install livewire |
Now use the npm commands simultaneously to get done the build compilation task.
1 |
npm install && npm run dev |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
Create Livewire CRUD Components
Now in this step we will create a livewire crud component using following laravel artisan command:
1 |
php artisan make:livewire crud |
The above command will generate two files, one in Http and another in resources directories.
1 2 3 |
CLASS: app/Http/Livewire/Crud.php VIEW: resources/views/livewire/crud.blade.php |
Update CRUD Component
Now you need to define the crud methods inside the livewire crud component file; open and update the given code in the app/Http/Livewire/Crud.phpfile.
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 |
<?php namespace App\Http\Livewire; use Livewire\Component; use App\Models\Student; class Crud extends Component { public $students, $name, $email, $mobile, $student_id; public $isModalOpen = 0; public function render() { $this->students = Student::all(); return view('livewire.crud'); } public function create() { $this->resetCreateForm(); $this->openModalPopover(); } public function openModalPopover() { $this->isModalOpen = true; } public function closeModalPopover() { $this->isModalOpen = false; } private function resetCreateForm(){ $this->name = ''; $this->email = ''; $this->mobile = ''; } public function store() { $this->validate([ 'name' => 'required', 'email' => 'required', 'mobile' => 'required', ]); Student::updateOrCreate(['id' => $this->student_id], [ 'name' => $this->name, 'email' => $this->email, 'mobile' => $this->mobile, ]); session()->flash('message', $this->student_id ? 'Student updated.' : 'Student created.'); $this->closeModalPopover(); $this->resetCreateForm(); } public function edit($id) { $student = Student::findOrFail($id); $this->student_id = $id; $this->name = $student->name; $this->email = $student->email; $this->mobile = $student->mobile; $this->openModalPopover(); } public function delete($id) { Student::find($id)->delete(); session()->flash('message', 'Studen deleted.'); } } |
Create Routes
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Livewire\Crud; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('students', Crud::class); |
If you are getting a “Route [dashboard] not defined.” error, open the resources/views/navigation-menu.blade.php file and comment entire code available in the file.
Create Blade View
Now open resources/views/livewire/crud.blade.php file.
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 |
<x-slot name="header"> <h2 class="text-center">Laravel 8 Livewire CRUD Demo</h2> </x-slot> <div class="py-12"> <div class="max-w-7xl mx-auto sm:px-6 lg:px-8"> <div class="bg-white overflow-hidden shadow-xl sm:rounded-lg px-4 py-4"> @if (session()->has('message')) <div class="bg-teal-100 border-t-4 border-teal-500 rounded-b text-teal-900 px-4 py-3 shadow-md my-3" role="alert"> <div class="flex"> <div> <p class="text-sm">{{ session('message') }}</p> </div> </div> </div> @endif <button wire:click="create()" class="my-4 inline-flex justify-center w-full rounded-md border border-transparent px-4 py-2 bg-red-600 text-base font-bold text-white shadow-sm hover:bg-red-700"> Create Student </button> @if($isModalOpen) @include('livewire.create') @endif <table class="table-fixed w-full"> <thead> <tr class="bg-gray-100"> <th class="px-4 py-2 w-20">No.</th> <th class="px-4 py-2">Name</th> <th class="px-4 py-2">Email</th> <th class="px-4 py-2">Mobile</th> <th class="px-4 py-2">Action</th> </tr> </thead> <tbody> @foreach($students as $student) <tr> <td class="border px-4 py-2">{{ $student->id }}</td> <td class="border px-4 py-2">{{ $student->name }}</td> <td class="border px-4 py-2">{{ $student->email}}</td> <td class="border px-4 py-2">{{ $student->mobile}}</td> <td class="border px-4 py-2"> <button wire:click="edit({{ $student->id }})" class="flex px-4 py-2 bg-gray-500 text-gray-900 cursor-pointer">Edit</button> <button wire:click="delete({{ $student->id }})" class="flex px-4 py-2 bg-red-100 text-gray-900 cursor-pointer">Delete</button> </td> </tr> @endforeach </tbody> </table> </div> </div> </div> |
Now create new file name it create.blade.php file inside the resources/views/livewire/ then put the following code in the resources/views/livewire/create.blade.php file.
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 |
<div class="fixed z-10 inset-0 overflow-y-auto ease-out duration-400"> <div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0"> <div class="fixed inset-0 transition-opacity"> <div class="absolute inset-0 bg-gray-500 opacity-75"></div> </div> <div class="inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full" role="dialog" aria-modal="true" aria-labelledby="modal-headline"> <form> <div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4"> <div class=""> <div class="mb-4"> <label for="exampleFormControlInput1" class="block text-gray-700 text-sm font-bold mb-2">Name</label> <input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="exampleFormControlInput1" placeholder="Enter Name" wire:model="name"> @error('name') <span class="text-red-500">{{ $message }}</span>@enderror </div> <div class="mb-4"> <label for="exampleFormControlInput2" class="block text-gray-700 text-sm font-bold mb-2">Email:</label> <textarea class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="exampleFormControlInput2" wire:model="email" placeholder="Enter Email"></textarea> @error('email') <span class="text-red-500">{{ $message }}</span>@enderror </div> <div class="mb-4"> <label for="exampleFormControlInput2" class="block text-gray-700 text-sm font-bold mb-2">Mobile:</label> <textarea class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="exampleFormControlInput2" wire:model="mobile" placeholder="Enter Mobile"></textarea> @error('mobile') <span class="text-red-500">{{ $message }}</span>@enderror </div> </div> </div> <div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse"> <span class="flex w-full rounded-md shadow-sm sm:ml-3 sm:w-auto"> <button wire:click.prevent="store()" type="button" class="inline-flex justify-center w-full rounded-md border border-transparent px-4 py-2 bg-red-600 text-base leading-6 font-bold text-white shadow-sm hover:bg-red-700 focus:outline-none focus:border-green-700 focus:shadow-outline-green transition ease-in-out duration-150 sm:text-sm sm:leading-5"> Store </button> </span> <span class="mt-3 flex w-full rounded-md shadow-sm sm:mt-0 sm:w-auto"> <button wire:click="closeModalPopover()" type="button" class="inline-flex justify-center w-full rounded-md border border-gray-300 px-4 py-2 bg-white text-base leading-6 font-bold text-gray-700 shadow-sm hover:text-gray-700 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue transition ease-in-out duration-150 sm:text-sm sm:leading-5"> Close </button> </span> </div> </form> </div> </div> </div> |
Start 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://127.0.0.1:8000/students |