Let’s build a fully working CRUD with image upload in Laravel 12 from scratch, step by step, without errors. I’ll provide migration, model, controller, routes, views, storage setup, and Bootstrap layout.
Step: 1 Create Laravel Project
Step: 2 Create Products Migration & Table
Edit database/migrations/xxxx_create_products_table.php:
Run migration:
Step : 3 Create Product Model
Edit app/Models/Product.php:
Step: 4 Create ProductController
Edit app/Http/Controllers/ProductController.php:
Step: 5 Routes
Edit routes/web.php:
Step :6 Create Layout
resources/views/layouts/app.blade.php:
Step 7: Blade Views
index.blade.php
create.blade.php
edit.blade.php
Step: 8 Create Upload Folder
Ensure folder is writable:
Start the server:
php
composer create-project laravel/laravel laravel_crud
cd laravel_crud
bash
php artisan make:migration create_products_table --create=products
php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description')->nullable();
$table->decimal('price', 10, 2);
$table->string('image')->nullable();
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('products');
}
};
bash
php artisan migrate
php
php artisan make:model Product
php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['name', 'description', 'price', 'image'];
}
php
php artisan make:controller ProductController --resource
php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
$products = Product::latest()->paginate(10);
return view('products.index', compact('products'));
}
public function create()
{
return view('products.create');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'price' => 'required|numeric',
'description' => 'nullable|string',
'image' => 'required|image|mimes:jpg,jpeg,png,webp|max:2048',
]);
$imageName = null;
if ($request->hasFile('image')) {
$imageName = time().'.'.$request->image->extension();
$request->image->move(public_path('uploads/products'), $imageName);
}
Product::create([
'name' => $request->name,
'price' => $request->price,
'description' => $request->description,
'image' => $imageName,
]);
return redirect()->route('products.index')->with('success', 'Product created successfully!');
}
public function edit($id)
{
$product = Product::findOrFail($id);
return view('products.edit', compact('product'));
}
public function update(Request $request, $id)
{
$product = Product::findOrFail($id);
$request->validate([
'name' => 'required|string|max:255',
'price' => 'required|numeric',
'description' => 'nullable|string',
'image' => 'nullable|image|mimes:jpg,jpeg,png,webp|max:2048',
]);
$product->name = $request->name;
$product->price = $request->price;
$product->description = $request->description;
if ($request->hasFile('image')) {
// Delete old image
if ($product->image && file_exists(public_path('uploads/products/'.$product->image))) {
unlink(public_path('uploads/products/'.$product->image));
}
// Upload new image
$imageName = time().'.'.$request->image->extension();
$request->image->move(public_path('uploads/products'), $imageName);
$product->image = $imageName;
}
$product->save();
return redirect()->route('products.index')->with('success', 'Product updated successfully!');
}
public function destroy($id)
{
$product = Product::findOrFail($id);
if ($product->image && file_exists(public_path('uploads/products/'.$product->image))) {
unlink(public_path('uploads/products/'.$product->image));
}
$product->delete();
return redirect()->route('products.index')->with('success', 'Product deleted successfully!');
}
}
Edit routes/web.php:
php
use App\Http\Controllers\ProductController;
Route::resource('products', ProductController::class);
resources/views/layouts/app.blade.php:
blade
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Laravel 12 CRUD</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark mb-4">
<div class="container">
<a class="navbar-brand" href="{{ route('products.index') }}">CRUD App</a>
</div>
</nav>
<div class="container">
@yield('content')
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
index.blade.php
blade
@extends('layouts.app')
@section('content')
<div class="d-flex justify-content-between mb-3">
<h3>Products</h3>
<a href="{{ route('products.create') }}" class="btn btn-primary">Add Product</a>
</div>
@if(session('success'))
<div class="alert alert-success">{{ session('success') }}</div>
@endif
<table class="table table-bordered">
<thead class="table-dark">
<tr>
<th>#</th>
<th>Name</th>
<th>Image</th>
<th>Price</th>
<th width="200px">Action</th>
</tr>
</thead>
<tbody>
@foreach($products as $product)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $product->name }}</td>
<td>
@if($product->image)
<img src="{{ asset('uploads/products/'.$product->image) }}" width="70">
@endif
</td>
<td>${{ $product->price }}</td>
<td>
<a href="{{ route('products.edit', $product->id) }}" class="btn btn-warning btn-sm">Edit</a>
<form action="{{ route('products.destroy', $product->id) }}" method="POST" style="display:inline;">
@csrf @method('DELETE')
<button class="btn btn-danger btn-sm" onclick="return confirm('Delete?')">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
{{ $products->links() }}
@endsection
blade
@extends('layouts.app')
@section('content')
<h3>Add Product</h3>
<form action="{{ route('products.store') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="mb-3">
<label>Name</label>
<input type="text" name="name" class="form-control" required>
</div>
<div class="mb-3">
<label>Description</label>
<textarea name="description" class="form-control"></textarea>
</div>
<div class="mb-3">
<label>Price</label>
<input type="number" name="price" class="form-control" required>
</div>
<div class="mb-3">
<label>Image</label>
<input type="file" name="image" class="form-control" required>
</div>
<button class="btn btn-success">Save</button>
</form>
@endsection
blade
@extends('layouts.app')
@section('content')
<h3>Edit Product</h3>
<form action="{{ route('products.update', $product->id) }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="mb-3">
<label>Name</label>
<input type="text" name="name" value="{{ $product->name }}" class="form-control" required>
</div>
<div class="mb-3">
<label>Description</label>
<textarea name="description" class="form-control">{{ $product->description }}</textarea>
</div>
<div class="mb-3">
<label>Price</label>
<input type="number" name="price" value="{{ $product->price }}" class="form-control" required>
</div>
<div class="mb-3">
<label>Image</label><br>
@if($product->image)
<img src="{{ asset('uploads/products/'.$product->image) }}" width="100" class="mb-2">
@endif
<input type="file" name="image" class="form-control">
</div>
<button class="btn btn-primary">Update</button>
</form>
@endsection
bash
mkdir -p public/uploads/products
bash
chmod -R 777 public/uploads/products
bash
php artisan serve