Laravel 11 + Vue Js CRUD Operation with Inertia from scratch step by step tutorial
Table of Contents
Introduction
This tutorial will guide you through creating a complete CRUD (Create, Read, Update, Delete) application using Laravel 11, Vue.js, and Inertia.js. By the end of this guide, you'll be able to set up a functional application that allows users to manage products seamlessly.
Step 1: Set Up Your Environment
Before diving into the CRUD operations, ensure that you have the following prerequisites:
- Laravel 11 installed on your machine.
- Vue.js and Inertia.js properly set up in your Laravel project.
- A working database connection configured in your
.env
file.
If you need guidance on setting up your Laravel environment, refer to the additional video linked in the description.
Step 2: Create Routes for Resource Controller
- Open the
routes/web.php
file in your Laravel application. - Add the resource route for products:
Route::resource('products', ProductController::class);
Step 3: Generate the Product Controller
- Open your terminal.
- Run the following Artisan command to create a resource controller:
php artisan make:controller ProductController --resource
- This command will create a controller with methods for all CRUD operations.
Step 4: Create Model and Migration
- Create a model and migration file for products:
php artisan make:model Product -m
- Open the migration file located in
database/migrations
to define the product table structure. Add the fields:Schema::create('products', function (Blueprint $table) { $table->id(); $table->string('name'); $table->integer('price'); $table->timestamps(); });
- Run the migration to create the table:
php artisan migrate
Step 5: Define the Product Model
- Open the
app/Models/Product.php
file. - Define the fillable fields:
protected $fillable = ['name', 'price'];
Step 6: Implement Controller Methods
- In
ProductController
, implement theindex
,create
,store
,edit
,update
, anddestroy
methods. - Example for the
index
method:public function index() { $products = Product::all(); return inertia('Product/Index', ['products' => $products]); }
- Implement similar logic for other methods. Use Inertia to render the views.
Step 7: Create Vue Components
- Create the Vue components for listing products, creating a product, editing a product, and showing product details.
- In
resources/js/Pages/Product/Index.vue
, set up a table to display products:<template> <div> <h5>Product List</h5> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Price</th> <th>Actions</th> </tr> </thead> <tbody> <tr v-for="product in products" :key="product.id"> <td>{{ product.id }}</td> <td>{{ product.name }}</td> <td>{{ product.price }}</td> <td> <Link :href="route('products.edit', product.id)">Edit</Link> <button @click="deleteProduct(product.id)">Delete</button> </td> </tr> </tbody> </table> </div> </template>
Step 8: Handling Product Creation and Errors
- In your
Create.vue
component, implement a form to add new products. - Use
v-model
for inputs and handle form submission with Inertia:<form @submit.prevent="submit"> <input v-model="form.name" placeholder="Product Name" /> <input v-model="form.price" type="number" placeholder="Product Price" /> <button type="submit">Save Product</button> </form>
Step 9: Implement Validation and Flash Messages
- In the
store
method ofProductController
, validate the incoming request:$request->validate([ 'name' => 'required|string|max:255', 'price' => 'required|integer', ]);
- Use session flash messages to inform users of successful operations.
Step 10: Implement Update and Delete Functionality
- In the
edit
method, retrieve the product using its ID and return it to the edit view. - Update the product in the
update
method after validating the input. - For deletion, confirm the action and use:
$product->delete();
Conclusion
You have successfully set up a CRUD application using Laravel 11, Vue.js, and Inertia.js. This tutorial covered the essential steps from routing to implementing CRUD operations. Next, consider enhancing your application with features like authentication, pagination, and more complex validations to improve user experience further.