0% found this document useful (0 votes)
24 views7 pages

Laravel E-commerce CRUD

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

Creating Migrations

Step 1: Create the Product Migration and Model


Generate the migration and model:

php artisan make:model Product -m

database/migrations/<timestamp>_create_products_table.php:

public function up()


{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description')->nullable();
$table->decimal('price', 10, 2);
$table->integer('stock');
$table->timestamps();
});
}

After doing the pasting above, run the below command to create the database table:

php artisan migrate


Making Controllers
php artisan make:controller ProductController

ProductController:

use App\Models\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)


{
$request->validate([
'name' => 'required',
'price' => 'required|numeric',
'stock' => 'required|integer',
]);

Product::create($request->all());
return redirect()->route('products.index')->with('success', 'Product created successfully.');
}

public function edit(Product $product)


{
return view('products.edit', compact('product'));
}

public function update(Request $request, Product $product)


{
$request->validate([
'name' => 'required',
'price' => 'required|numeric',
'stock' => 'required|integer',
]);

$product->update($request->all());
return redirect()->route('products.index')->with('success', 'Product updated successfully.');
}

public function destroy(Product $product)


{
$product->delete();
return redirect()->route('products.index')->with('success', 'Product deleted successfully.');
}
}

Update the file at App\Http\Controllers\ProductController:


use App\Http\Controllers\ProductController;

Route::resource('products', ProductController::class);
Modify the Layouts
1. Create the Layout (resources/views/layouts/app.blade.php):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>E-commerce CRUD</title>
@vite('resources/css/app.css')
</head>
<body class="bg-gray-100 text-gray-800">
<div class="container mx-auto p-4">
@if (session('success'))
<div class="bg-green-200 text-green-800 p-3 rounded mb-4">
{{ session('success') }}
</div>
@endif
@yield('content')
</div>
</body>
</html>

2. Index View (resources/views/products/index.blade.php):


@extends('layouts.app')

@section('content')
<div class="mb-4">
<h1 class="text-2xl font-bold">Products</h1>
<a href="{{ route('products.create') }}" class="bg-blue-500 text-white px-4 py-2
rounded">Create Product</a>
</div>

<table class="table-auto w-full bg-white rounded shadow">


<thead>
<tr class="bg-gray-200 text-left">
<th class="px-4 py-2">Name</th>
<th class="px-4 py-2">Description</th>
<th class="px-4 py-2">Price</th>
<th class="px-4 py-2">Stock</th>
<th class="px-4 py-2">Actions</th>
</tr>
</thead>
<tbody>
@foreach ($products as $product)
<tr class="border-b">
<td class="px-4 py-2">{{ $product->name }}</td>
<td class="px-4 py-2">{{ $product->description }}</td>
<td class="px-4 py-2">${{ $product->price }}</td>
<td class="px-4 py-2">{{ $product->stock }}</td>
<td class="px-4 py-2">
<a href="{{ route('products.edit', $product) }}" class="text-blue-500">Edit</a>
<form action="{{ route('products.destroy', $product) }}" method="POST" class="inline-
block">
@csrf
@method('DELETE')
<button type="submit" class="text-red-500">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
@endsection

3. Create View (resources/views/products/create.blade.php):


@extends('layouts.app')

@section('content')
<div>
<h1 class="text-2xl font-bold mb-4">Create Product</h1>
<form action="{{ route('products.store') }}" method="POST" class="bg-white p-4 rounded
shadow">
@csrf
<div class="mb-4">
<label class="block text-sm font-medium">Name</label>
<input type="text" name="name" class="border rounded w-full p-2" required>
</div>
<div class="mb-4">
<label class="block text-sm font-medium">Description</label>
<textarea name="description" class="border rounded w-full p-2"></textarea>
</div>
<div class="mb-4">
<label class="block text-sm font-medium">Price</label>
<input type="number" name="price" step="0.01" class="border rounded w-full p-2"
required>
</div>
<div class="mb-4">
<label class="block text-sm font-medium">Stock</label>
<input type="number" name="stock" class="border rounded w-full p-2" required>
</div>
<button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded">Create</button>
</form>
</div>
@endsection

4. Edit View (resources/views/products/edit.blade.php):


@extends('layouts.app')

@section('content')
<div>
<h1 class="text-2xl font-bold mb-4">Edit Product</h1>
<form action="{{ route('products.update', $product) }}" method="POST" class="bg-white p-4
rounded shadow">
@csrf
@method('PUT')
<div class="mb-4">
<label class="block text-sm font-medium">Name</label>
<input type="text" name="name" value="{{ $product->name }}" class="border rounded
w-full p-2" required>
</div>
<div class="mb-4">
<label class="block text-sm font-medium">Description</label>
<textarea name="description" class="border rounded w-full p-2">{{ $product-
>description }}</textarea>
</div>
<div class="mb-4">
<label class="block text-sm font-medium">Price</label>
<input type="number" name="price" step="0.01" value="{{ $product->price }}"
class="border rounded w-full p-2" required>
</div>
<div class="mb-4">
<label class="block text-sm font-medium">Stock</label>
<input type="number" name="stock" value="{{ $product->stock }}"

class="border rounded w-full p-2" required> Update

You might also like