Laravel Livewire Crud Tutorial

By Sumeet Shroff
Last Updated On : March 4, 2023
Laravel Livewire Crud Tutorial

Laravel Livewire is a powerful framework for creating dynamic web applications. It offers an intuitive, modern way to build robust CRUD (Create Read Update Delete) applications quickly and easily. This Laravel Livewire CRUD tutorial will help you understand the fundamentals of the framework and get started with building a simple CRUD application. First, let's take a look at what makes Laravel Livewire so powerful. At its core, it is built on top of the popular PHP framework Laravel which provides many features such as routing, authentication, and more that can be used in your application. Furthermore, it uses components that allow developers to quickly create interactive user interfaces with minimal effort. Finally, it utilizes server-side rendering which reduces page load times and improves performance by only updating parts of the page when they are changed or interacted with by the user instead of reloading the entire page over again each time an action is performed. Now that we have seen what makes Laravel Livewire so great let's dive into creating our first CRUD application using it! We'll start off by setting up our project: 1). Create a new laravel project using composer:

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

2). Install livewire via composer:

composer require livewire/livewire

3). Add a service provider in config/app.php for live wire :

LiveWire\LiveWireServiceProvider::class

Now we are ready to start developing our application! In this tutorial we will build an example blog posts manager app where users can view posts list , add new post , update existing post or delete post . To do this we need three components : PostList , PostForm , PostDelete . Let's create them ! 4). Create component folder under App directory; Open terminal type following command :

php artisan make:liveware PostList

5). Open newly created component file located in App\Http\Liveware folder; Here you'll find basic template structure generated based on component name ; You need to define view & logic function inside your component class like below code -

public function render() { return view('liveware.'.$this->name); }
public function mount(){ // Your logic goes here }

The mount() method plays important role while dealing with data binding & form submission process . Inside this method you can write your own business logic for fetching / submitting data from Database etc . For example if you want to fetch all blog posts from database & display in list then you would write following code - $this->posts = Posts::all(); Now we need to define view template for displaying blog posts list ; Inside blade file (resources/views/liveview/$componentName ) add following code -

@foreach ($posts as $post)
{{$post->title}}
{{ substr($post->content}, 0 , 250 ) }}
@endforeach

Here We have successfully completed ‘Post List’ Component part ! Similarly other two components such as ‘Post Form’ & ‘Post Delete’ must be created each having their own custom business logics (eg – Creating / Updating / Deleting Data ). Finally our application is ready ! But now how do we run ? Simple – We just need one main layout blade file where all three Components can be called at once like below code -

@livewire('postlist')
@include('postsform')
@include('postdelete')

. So now when ever any change happens related any component those changes get reflected instantly without needing full page reloads thanks to Server Side Rendering feature provided by Laravel LiveWire Framework . Congratulations !! You have successfully developed your first Crud Application using Laravel LiveWire!

Categories

Latest Blogs

Ready to Amplify Your Services with our Premium White Label Web Design

Are you looking to expand your service offerings without the overhead of an in-house design team? Our white label web design services are the perfect solution. We provide top-notch, fully customizable web designs that seamlessly blend with your brand, allowing you to offer additional value to your clients.
Contact Us Today to Start Partnering with Our White Label Web Design Experts!
© 2024 · Prateeksha Web Design. Built with Gatsby All rights reserved | Privacy Policy
Designed by Prateeksha Web