Laravel 12 Livewire CRUD Example with Validation | asadmukhtar.info
Step-by-Step Guide to Setting Up Authentication in Laravel 12 with Breeze   |   Manual Authentication in Laravel 12: Step-by-Step Guide   |   How to Build a REST API in Laravel 12 with Sanctum   |   Laravel 12 CRUD Application with Image Upload   |   Laravel 12 Multi-Auth System: Admin & User Login   |   How to Integrate Stripe Payment Gateway in Laravel 12   |   Building a Role-Based Access Control (RBAC) in Laravel 12   |   How to Use Laravel 12 Queues and Jobs for Background Tasks   |   Laravel 12 Livewire CRUD Example with Validation   |   Email Verification and Password Reset in Laravel 12   |   How to Use Laravel 12 API with Vue.js 3   |   Laravel 12 AJAX CRUD with jQuery and Bootstrap   |   Laravel 12 Multi-Language Website Setup   |   React Best Practices for 2025: Performance, SEO, and Scalability   |   How to Build a Full-Stack MERN App: A Step-by-Step Guide   |   React State Management: Redux vs. Context API vs. Recoil   |   Server-Side Rendering (SSR) in React with Next.js for SEO   |   How to Optimize React Apps for Faster Load Times   |   Building a REST API with Node.js and Express for a React App   |   Integrating JWT Authentication in React and Node.js (MERN Stack)   |   Real-time Chat App with React, Node.js, and Socket.io   |   How to Deploy a MERN Stack Application on AWS or Vercel   |   Connecting React Frontend to a Node.js Backend with Axios   |   Laravel Implement Flash Messages Example   |   How to integrate Angular 19 with Node.js and Express for full-stack development   |   Best practices for connecting Angular 19 frontend with Laravel API   |   Step-by-step guide to upgrading an existing project to Angular 19   |   How to implement authentication in Angular 19 using JWT and Firebase   |   Optimizing server-side rendering in Angular 19 with route-level render modes   |   Using Angular 19 signals for state management in large applications   |   How to create standalone components in Angular 19 for modular architecture   |   Building a CRUD application in Angular 19 with MongoDB and Express   |   Implementing lazy loading in Angular 19 to improve performance   |   How to integrate Angular 19 with GraphQL for efficient data fetching   |   Vue 3 Composition API vs Options API: A Comprehensive Comparison   |   Fetching and Displaying Data from APIs in Vue.js with Axios   |   Building a Todo App in Vue.js with Local Storage Integration   |   Handling Forms and Validation in Vue.js Using VeeValidate   |   State Management in Vue.js Applications Using Vuex   |   10 Most Important Tasks Every MERN Stack Developer Should Master   |   How to Build a Full-Stack CRUD App with MERN Stack   |   Best Practices for Authentication & Authorization in MERN Stack   |   1. MEAN Stack vs. MERN Stack: Which One Should You Choose in 2025   |   Top 10 Node.js Best Practices for Scalable and Secure Applications   |   How to Build a REST API with Laravel and Node.js (Step-by-Step Guide)   |   Mastering Angular and Express.js for Full-Stack Web Development   |   Top 10 Daily Tasks Every Frontend Developer Should Practice   |   Essential Backend Development Tasks to Boost Your Coding Skills   |   Real-World Mini Projects for Practicing React.js Daily   |   Laravel Developer Task List: Beginner to Advanced Challenges   |   How to Assign Effective Tasks to Your Intern Developers   |   10 Must-Try Tasks to Master JavaScript Fundamentals   |   Practical CSS Challenges That Improve Your UI Design Skills   |   Top Tasks to Learn API Integration in React and Angular   |   Best Task Ideas for a 30-Day Web Development Challenge   |   Top Git and GitHub Tasks Every Developer Should Know   |   30-Day Task Plan for Web Development Interns   |   Weekly Task Schedule for Junior Developers in a Startup   |   How to Track Progress with Development Tasks for Interns   |   What Tasks Should You Give to Interns in a MERN Stack Project   |   Build These 5 Projects to Master React Routing   |   Task-Based Learning: Become a Full-Stack Developer in 90 Days   |   Daily Coding Tasks That Will Sharpen Your Logical Thinking   |   Top 7 Backend Task Ideas to Practice With Node.js and MongoDB   |  

Laravel 12 Livewire CRUD Example with Validation

Livewire is a full-stack framework for Laravel that makes building dynamic interfaces simple, without leaving the comfort of Laravel. It allows you to create modern, reactive user interfaces without writing a lot of JavaScript. Livewire components are great for building CRUD (Create, Read, Update, Delete) applications, and they work seamlessly with Laravel’s backend.

This tutorial will walk you through creating a simple CRUD application using Laravel 12 and Livewire. We'll implement validation and display error messages in a user-friendly way.

Step-by-Step Guide to Implement Laravel 12 Livewire CRUD with Validation

Step 1: Install Laravel 12

First, make sure Laravel is installed. You can install Laravel via Composer:

Laravel Tutorial

After installation, navigate into the project folder:

cd laravel-livewire-crud

Step 2: Install Livewire

Livewire can be installed via Composer. Run the following command:

composer require livewire/livewire

Once installed, you need to add Livewire's assets to your layout file. Open resources/views/layouts/app.blade.php and add the following lines before the </head> tag:

@livewireStyles

And just before the </body> tag, add this:

@livewireScripts

Step 3: Create the Livewire Component

Now we will create a Livewire component that will handle the CRUD operations. Run the following command to create a component:

php artisan make:livewire ProductCrud

This creates two files:

  • app/Http/Livewire/ProductCrud.php (the Livewire component)
  • resources/views/livewire/product-crud.blade.php (the corresponding view)

Step 4: Create the Product Model and Migration

Next, we will create a Product model and migration file:

php artisan make:model Product -m

This generates a Product model and a migration file in the database/migrations folder. Open the migration file and define the schema for the products table:

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->decimal('price', 8, 2);
        $table->timestamps();
    });
}

Step 5: Create CRUD Operations in Livewire Component

Now, we will add the CRUD operations inside the ProductCrud.php Livewire component.

Edit app/Http/Livewire/ProductCrud.php:

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Product;
use Livewire\WithFileUploads;

class ProductCrud extends Component
{
    use WithFileUploads;

    public $name, $price, $productId;
    public $isEditMode = false;

    protected $rules = [
        'name' => 'required|string|max:255',
        'price' => 'required|numeric|min:0',
    ];

    public function render()
    {
        return view('livewire.product-crud', [
            'products' => Product::all(),
        ]);
    }

    public function store()
    {
        $this->validate();

        Product::create([
            'name' => $this->name,
            'price' => $this->price,
        ]);

        session()->flash('message', 'Product Created Successfully!');
        $this->reset();
    }

    public function edit($id)
    {
        $this->isEditMode = true;
        $product = Product::find($id);
        $this->productId = $product->id;
        $this->name = $product->name;
        $this->price = $product->price;
    }

    public function update()
    {
        $this->validate();

        $product = Product::find($this->productId);
        $product->update([
            'name' => $this->name,
            'price' => $this->price,
        ]);

        session()->flash('message', 'Product Updated Successfully!');
        $this->reset();
        $this->isEditMode = false;
    }

    public function delete($id)
    {
        Product::find($id)->delete();
        session()->flash('message', 'Product Deleted Successfully!');
    }
}

Step 6: Create the Livewire Component View

Next, we will create the view for the Livewire component in resources/views/livewire/product-crud.blade.php:

<div>
    @if (session()->has('message'))
        <div class="alert alert-success">
            {{ session('message') }}
        </div>
    @endif

    <form wire:submit.prevent="{{ $isEditMode ? 'update' : 'store' }}">
        <div class="form-group">
            <label for="name">Product Name</label>
            <input type="text" wire:model="name" class="form-control" id="name" placeholder="Enter Product Name">
            @error('name') <span class="text-danger">{{ $message }}</span> @enderror
        </div>

        <div class="form-group">
            <label for="price">Product Price</label>
            <input type="text" wire:model="price" class="form-control" id="price" placeholder="Enter Product Price">
            @error('price') <span class="text-danger">{{ $message }}</span> @enderror
        </div>

        <button type="submit" class="btn btn-primary">{{ $isEditMode ? 'Update' : 'Create' }} Product</button>
    </form>

    <hr>

    <h3>Products</h3>
    <table class="table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Price</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            @foreach($products as $product)
                <tr>
                    <td>{{ $product->name }}</td>
                    <td>${{ $product->price }}</td>
                    <td>
                        <button wire:click="edit({{ $product->id }})" class="btn btn-warning btn-sm">Edit</button>
                        <button wire:click="delete({{ $product->id }})" class="btn btn-danger btn-sm">Delete</button>
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>
</div>

Step 7: Add Route and View

Now, you need to add a route to display the Livewire component. Open routes/web.php and add:

use App\Http\Livewire\ProductCrud;

Route::get('/products', ProductCrud::class);

Step 8: Testing the CRUD Operations

To test the CRUD functionality, visit http://your-app.local/products in the browser. You should be able to:

  • Create products
  • View products in a table
  • Edit products
  • Delete products

Conclusion

In this tutorial, we have created a simple Livewire CRUD application in Laravel 12, with validation. We used Livewire components to manage the frontend and backend interactions for creating, reading, updating, and deleting products. Validation rules were applied to ensure correct data input, and feedback was displayed to users upon successful or failed operations.

Livewire makes it easy to build dynamic applications without writing much JavaScript, and its seamless integration with Laravel makes it a powerful tool for modern web development.


Related Tutorials

Laravel 12 Livewire CRUD Example with Validation