Nuevo Documento de Microsoft Word
Nuevo Documento de Microsoft Word
Powered By Play
Unmute
Loaded: 1.68%
Fullscreen
In this tutorial, i will show you laravel 8 inertia js crud example. We will use laravel 8
inertia js crud with jetstream & tailwind css. you will learn laravel 8 inertia js crud with
modal. i explained simply about laravel 8 jetstream inertia js vue js crud application
example.
Laravel 8 jetstream designed by Tailwind CSS and they provide auth using
inertia js and Inertia. i will show you how to create module with inertia.js vue
js on default jetstream auth in laravel 8.
Here, bellow i written step by step, so you can easily start simple post master
with your existing step up of laravel 8 jetstream auth with tailwind css. you
just need to follow few bellow step and you will get layout as like bellow:
Preview:
List View:
Create View:
Update View:
now, we need to create authentication using bellow command. you can create
basic login, register and email verification. if you want to create team
management then you have to pass addition parameter. you can see bellow
commands:
OR
npm install
Here, we need create database migration for files table and also we will create
model for files table.
Migration:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* @return void
*/
{
Schema::create('posts', function (Blueprint
$table) {
$table->id();
$table->string('title');
$table->text('body');
$table->timestamps();
});
/**
* @return void
*/
Schema::dropIfExists('posts');
App/Models/Post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\
HasFactory;
use Illuminate\Database\Eloquent\Model;
use HasFactory;
/**
* @var array
*/
protected $fillable = [
'title', 'body'
];
In third step, we will create routes for crud app. so create resource route here.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
/*
|---------------------------------------------------
-----------------------
| Web Routes
|---------------------------------------------------
-----------------------
*/
Route::resource('posts', PostController::class);
In this step, we will create postcontroller file and add following code on it.
app/Http/Controllers/PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Inertia\Inertia;
use App\Models\Post;
use Illuminate\Support\Facades\Validator;
/**
* @return Response
*/
$data = Post::all();
/**
* Show the form for creating a new resource.
* @return Response
*/
Validator::make($request->all(), [
])->validate();
Post::create($request->all());
return redirect()->back()
/**
* @return Response
*/
Validator::make($request->all(), [
])->validate();
if ($request->has('id')) {
Post::find($request->input('id'))-
>update($request->all());
return redirect()->back()
/**
* @return Response
*/
if ($request->has('id')) {
Post::find($request->input('id'))-
>delete();
return redirect()->back();
Here, we will share 'message' and 'errors' variable for success message and
validation error so. we need to share this variables on appservices provider as
like bellow:
app/Providers/AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Session;
use Inertia\Inertia;
/**
* @return void
*/
public function register()
/**
* @return void
*/
Inertia::share([
return Session::get('errors')
? Session::get('errors')-
>getBag('default')->getMessages()
: (object) [];
},
]);
Inertia::share('flash', function () {
return [
'message' =>
Session::get('message'),
];
});
Here, we need to create posts. vue file where we will write code to list of posts
and create and update model code.
resources/js/Pages/posts.vue
<template>
<app-layout>
<template #header>
</h2>
</template>
<div class="py-12">
<div class="flex">
<div>
<p class="text-
sm">{{ $page.flash.message }}</p>
</div>
</div>
</div>
<button @click="openModal()"
class="bg-blue-500 hover:bg-blue-700 text-white
font-bold py-2 px-4 rounded my-3">Create New
Post</button>
<table class="table-fixed w-
full">
<thead>
<tr class="bg-gray-100">
</tr>
</thead>
<tbody>
<tr v-for="row in data">
<td class="border
px-4 py-2">{{ row.id }}</td>
<td class="border
px-4 py-2">{{ row.title }}</td>
<td class="border
px-4 py-2">{{ row.body }}</td>
<td class="border
px-4 py-2">
<button
@click="edit(row)" class="bg-blue-500 hover:bg-blue-
700 text-white font-bold py-2 px-4
rounded">Edit</button>
<button
@click="deleteRow(row)" class="bg-red-500 hover:bg-
red-700 text-white font-bold py-2 px-4
rounded">Delete</button>
</td>
</tr>
</tbody>
</table>
</div>
<span class="hidden
sm:inline-block sm:align-middle sm:h-screen"></span>
<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="">
<div class="mb-4">
<label
for="exampleFormControlInput1" class="block text-
gray-700 text-sm font-bold mb-2">Title:</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
Title" v-model="form.title">
<div v-
if="$page.errors.title" class="text-red-
500">{{ $page.errors.title[0] }}</div>
</div>
<div class="mb-4">
<label
for="exampleFormControlInput2" class="block text-
gray-700 text-sm font-bold mb-2">Body:</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" v-model="form.body"
placeholder="Enter Body"></textarea>
<div v-
if="$page.errors.body" class="text-red-
500">{{ $page.errors.body[0] }}</div>
</div>
</div>
</div>
<button
wire:click.prevent="store()" type="button"
class="inline-flex justify-center w-full rounded-md
border border-transparent px-4 py-2 bg-green-600
text-base leading-6 font-medium text-white shadow-sm
hover:bg-green-500 focus:outline-none focus:border-
green-700 focus:shadow-outline-green transition
ease-in-out duration-150 sm:text-sm sm:leading-5" v-
show="!editMode" @click="save(form)">
Save
</button>
</span>
Update
</button>
</span>
<button
@click="closeModal()" 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-medium text-gray-700 shadow-sm hover:text-gray-
500 focus:outline-none focus:border-blue-300
focus:shadow-outline-blue transition ease-in-out
duration-150 sm:text-sm sm:leading-5">
Cancel
</button>
</span>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</app-layout>
</template>
<script>
export default {
components: {
AppLayout,
Welcome,
},
data() {
return {
editMode: false,
isOpen: false,
form: {
title: null,
body: null,
},
}
},
methods: {
openModal: function () {
this.isOpen = true;
},
closeModal: function () {
this.isOpen = false;
this.reset();
this.editMode=false;
},
reset: function () {
this.form = {
title: null,
body: null,
},
this.$inertia.post('/posts', data)
this.reset();
this.closeModal();
this.editMode = false;
},
this.openModal();
},
data._method = 'PUT';
this.$inertia.post('/posts/' +
data.id, data)
this.reset();
this.closeModal();
},
data._method = 'DELETE';
this.$inertia.post('/posts/' +
data.id, data)
this.reset();
this.closeModal();
</script>
now you can run app and login as user, then you can open bellow url:
Read Also: Laravel 8 Change Date Format Examples
//localhost:8000/post