How to add product add, update, edit, delete in Laravel with product table?
The process of creating a basic CRUD (Create, Read, Update, Delete) functionality for a product table in Laravel. First, make sure you have Laravel installed on your system.
Step 1: Database Setup
Create a migration for the product table:
bash code
php artisan make:migration create_products_table
Edit the generated migration file to define the columns of your products table. Then, run the migration:
bash code
php artisan migrate
Step 2: Model
Create a model for the Product:
bash code
php artisan make:model Product
Step 3: Routes
Define your routes in web.php:
php code
Route::resource('products', 'ProductController');
Step 4: Controller
Create a controller for the Product:
bash code
php artisan make:controller ProductController
In the ProductController, you can define methods for the CRUD operations:
php code
<?php
namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
$products = Product::all();
return view('products.index', compact('products'));
}
public function create()
{
return view('products.create');
}
public function store(Request $request)
{
$product = new Product();
$product->name = $request->input('name');
$product->description = $request->input('description');
// Set other fields as needed
$product->save();
return redirect()->route('products.index')->with('success', 'Product created successfully.');
}
public function edit($id)
{
$product = Product::find($id);
return view('products.edit', compact('product'));
}
public function update(Request $request, $id)
{
$product = Product::find($id);
$product->name = $request->input('name');
$product->description = $request->input('description');
// Update other fields as needed
$product->save();
return redirect()->route('products.index')->with('success', 'Product updated successfully.');
}
public function destroy($id)
{
$product = Product::find($id);
$product->delete();
return redirect()->route('products.index')->with('success', 'Product deleted successfully.');
}
}
Step 5: Views
Create the necessary views inside the resources/views/products directory: index.blade.php, create.blade.php, edit.blade.php.
index.blade.php
php
Copy code
@extends('layouts.app')
@section('content')
<h1>Products</h1>
<a href="{{ route('products.create') }}" class="btn btn-primary">Add Product</a>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach($products as $product)
<tr>
<td>{{ $product->name }}</td>
<td>{{ $product->description }}</td>
<td>
<a href="{{ route('products.edit', $product->id) }}" class="btn btn-warning">Edit</a>
<form action="{{ route('products.destroy', $product->id) }}" method="POST" style="display:inline;">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this product?')">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
@endsection
create.blade.php
php code
@extends('layouts.app')
@section('content')
<h1>Create Product</h1>
<form action="{{ route('products.store') }}" method="POST">
@csrf
<div class="form-group">
<label for="name">Product Name:</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="form-group">
<label for="description">Description:</label>
<textarea class="form-control" id="description" name="description" required></textarea>
</div>
<!-- Add other fields as needed -->
<button type="submit" class="btn btn-primary">Create Product</button>
</form>
@endsection
edit.blade.php
php code
@extends('layouts.app')
@section('content')
<h1>Edit Product</h1>
<form action="{{ route('products.update', $product->id) }}" method="POST">
@csrf
@method('PUT')
<div class="form-group">
<label for="name">Product Name:</label>
<input type="text" class="form-control" id="name" name="name" value="{{ $product->name }}" required>
</div>
<div class="form-group">
<label for="description">Description:</label>
<textarea class="form-control" id="description" name="description" required>{{ $product->description }}</textarea>
</div>
<!-- Add other fields as needed -->
<button type="submit" class="btn btn-primary">Update Product</button>
</form>
@endsection
Make sure to have appropriate styles and scripts in your layout file (layouts/app.blade.php).
That's it! You now have a basic CRUD functionality for a product table in Laravel. Customize the code as per your requirements.
Comments
Post a Comment