<?phpnamespaceApp\Livewire;useApp\Models\Post;useLivewire\Component;classPostsextendsComponent{public $posts, $title, $body, $post_id;public $updateMode =false;/** * The attributes that are mass assignable. * @vararray */privatefunctionresetInputFields() {$this->title ='';$this->body =''; }/** * The attributes that are mass assignable. * * @vararray */publicfunctionstore() { $validatedDate =$this->validate(['title'=>'required','body'=>'required' ]);Post::create($validatedDate);session()->flash('message','Post Created Successfully.');$this->resetInputFields(); }/** * The attributes that are mass assignable. * * @vararray */publicfunctionedit($id) { $post =Post::findOrFail($id);$this->post_id = $id;$this->title = $post->title;$this->body = $post->body;$this->updateMode =true; }/** * The attributes that are mass assignable. * * @vararray */publicfunctioncancel() {$this->updateMode =false;$this->resetInputFields(); }/** * The attributes that are mass assignable. * * @vararray */publicfunctionupdate() { $validatedDate =$this->validate(['title'=>'required','body'=>'required' ]); $post =Post::find($this->post_id); $post->update(['title'=>$this->title,'body'=>$this->body ]);$this->updateMode =false;session()->flash('message','Post Updated Successfully.');$this->resetInputFields(); }/** * The attributes that are mass assignable. * * @vararray */publicfunctiondelete($id) {Post::find($id)->delete();session()->flash('message','Post Deleted Successfully.'); }publicfunctionrender() {$this->posts =Post::all();returnview('livewire.posts'); }}
By Hardik Savani June 20, 2020 Category : LaravelPauseUnmuteLoaded: 2.69%FullscreenHi Dev,
This post is focused on laravel livewire crud example. This tutorial will give you simple example of crud application using livewire laravel example. we will help you to give example of laravel 7 livewire tutorial example. you can understand a concept of livewire crud laravel example.
Here, i will give you step by step crud operation using livewire in laravel 6, laravel 7, laravel 8 and laravel 9 example. you have to just follow few step to done this example.
Livewire is a full-stack framework for Laravel framework that makes building dynamic interfaces simple, without leaving the comfort of Laravel. if you are using livewire with laravel then you don't worry about writing jquery ajax code, livewire will help to write very simple way jquery ajax code using php. without page refresh laravel validation will works, form will submit etc.
In this example we will create post insert update delete module. we will take title and body as post field and you can create post, update post and delete that post.
So, let's follow bellow step and you will get bellow layout:
List View:
Create View:
Update View:
Step 1: Install Laravel 7
first of all we need to get fresh Laravel 7 version application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Create Migration and Model
Here, we need create database migration for files table and also we will create model for files table.
php artisan make:migration create_posts_table
Migration:
<?php use Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema; class CreatePostsTable extends Migration{ /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('body'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); }}
php artisan migrate
now we will create Post model by using following command:
php artisan make:model Post
App/Post.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model{ /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'title', 'body' ];}
Step 3: Install Livewire
now in this step, we will simply install livewire to our laravel 7 application using bellow command:
composer require livewire/livewire
Step 4: Create Post Component
Now here we will create livewire component using their command. so run bellow command to create post crud application component.
Here, we will write render(), resetInputFields(), store(), edit(), cancel(), update() and delete() method for our crud app.
So, let, update following file.
app/Http/Livewire/Post.php
<?php namespace App\Http\Livewire; use Livewire\Component;use App\Post; class Posts extends Component{ public $posts, $title, $body, $post_id; public $updateMode = false; /** * The attributes that are mass assignable. * * @var array */ public function render() { $this->posts = Post::all(); return view('livewire.posts'); } /** * The attributes that are mass assignable. * * @var array */ private function resetInputFields(){ $this->title = ''; $this->body = ''; } /** * The attributes that are mass assignable. * * @var array */ public function store() { $validatedDate = $this->validate([ 'title' => 'required', 'body' => 'required', ]); Post::create($validatedDate); session()->flash('message', 'Post Created Successfully.'); $this->resetInputFields(); } /** * The attributes that are mass assignable. * * @var array */ public function edit($id) { $post = Post::findOrFail($id); $this->post_id = $id; $this->title = $post->title; $this->body = $post->body; $this->updateMode = true; } /** * The attributes that are mass assignable. * * @var array */ public function cancel() { $this->updateMode = false; $this->resetInputFields(); } /** * The attributes that are mass assignable. * * @var array */ public function update() { $validatedDate = $this->validate([ 'title' => 'required', 'body' => 'required', ]); $post = Post::find($this->post_id); $post->update([ 'title' => $this->title, 'body' => $this->body, ]); $this->updateMode = false; session()->flash('message', 'Post Updated Successfully.'); $this->resetInputFields(); } /** * The attributes that are mass assignable. * * @var array */ public function delete($id) { Post::find($id)->delete(); session()->flash('message', 'Post Deleted Successfully.'); }}
Step 5: Update Blade Files
Here, we will update following list of files for our listing page, create page and update page.
We will update laravel welcome blade file so it will load crud example on our main url.
here, we will update welcome.blade.php file. in this file we will use @livewireStyles, @livewireScripts and @livewire('contact-form'). so let's add it.