This article goes in detailed on laravel 9 inertia js crud example. We will use laravel 9 inertia js crud with jetstream & tailwind css. step by step explain laravel 9 inertia js crud with modal. you can see laravel 9 jetstream inertia js crud application example.
Laravel 9 jetstream design 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 9.
Here, below i wrote step by step, so you can easily start a simple postmaster with your existing step up of laravel 9 jetstream auth with tailwind css. you just need to follow few below step and you will get the layout as like below:
List View:
Create View:
Update View:
Step 1: Install Laravel 9
This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:
Now, in this step, we need to use composer command to install jetstream, so let's run bellow command and install bellow library.
composer require laravel/jetstream
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:
php artisan jetstream:install inertia OR php artisan jetstream:install inertia --teams
Now, let's node js package:
npm install
let's run package:
npm run dev
now, we need to run migration command to create database table:
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; return new class 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/Models/Post.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model; class Post extends Model{ use HasFactory; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'title', 'body' ];}
Step 4: Create Route
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|--------------------------------------------------------------------------|| 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::resource('posts', PostController::class);
Step 5: Create Controller
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; class PostController extends Controller{ /** * Show the form for creating a new resource. * * @return Response */ public function index() { $data = Post::all(); return Inertia::render('posts', ['data' => $data]); } /** * Show the form for creating a new resource. * * @return Response */ public function store(Request $request) { Validator::make($request->all(), [ 'title' => ['required'], 'body' => ['required'], ])->validate(); Post::create($request->all()); return redirect()->back() ->with('message', 'Post Created Successfully.'); } /** * Show the form for creating a new resource. * * @return Response */ public function update(Request $request) { Validator::make($request->all(), [ 'title' => ['required'], 'body' => ['required'], ])->validate(); if ($request->has('id')) { Post::find($request->input('id'))->update($request->all()); return redirect()->back() ->with('message', 'Post Updated Successfully.'); } } /** * Show the form for creating a new resource. * * @return Response */ public function destroy(Request $request) { if ($request->has('id')) { Post::find($request->input('id'))->delete(); return redirect()->back(); } }}
Step 6: Share Inertia Var
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; class AppServiceProvider extends ServiceProvider{ /** * Register any application services. * * @return void */ public function register() { } /** * Bootstrap any application services. * * @return void */ public function boot() { Inertia::share([ 'errors' => function () { return Session::get('errors') ? Session::get('errors')->getBag('default')->getMessages() : (object) []; }, ]); Inertia::share('flash', function () { return [ 'message' => Session::get('message'), ]; }); }}
Step 7: Create Vue Page
Here, we need to create posts. vue file where we will write code to list of posts and create and update model code.