Creating a CRUD (Create, Read, Update, Delete) application is a fundamental task in web development. Laravel, with its powerful features and expressive syntax, makes building CRUD applications a breeze. In this tutorial, we'll guide you through the process of building a simple CRUD application with Laravel and MySQL.
Before starting, ensure you have the following:
- A working installation of Laravel.
- A MySQL database configured in Laravel.
- Basic knowledge of Laravel migrations, routes, controllers, and views.
Step 1: Set Up the Laravel Project
Create a new Laravel project using Composer:
composer create-project --prefer-dist laravel/laravel crud-app
cd crud-app
Step 2: Set Up the Database
Configure your database credentials in the .env
file. Create a migration for the "products" table:
php artisan make:migration create_products_table
In the migration file (database/migrations/YYYY_MM_DD_create_products_table.php
), define the schema for the "products" table:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->decimal('price', 8, 2);
$table->timestamps();
});
}
Run the migration to create the "products" table:
php artisan migrate
Step 3: Create the Model and Controller
Generate a model and a controller for the "Product" resource:
php artisan make:model Product -m
php artisan make:controller ProductController --resource
In the Product
model (app/Models/Product.php
), define the fillable fields:
protected $fillable = ['name', 'description', 'price'];
In the ProductController
, implement the CRUD methods:
use App\Models\Product;
use Illuminate\Http\Request;
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::create($request->all());
return redirect()->route('products.index')->with('success', 'Product created successfully.');
}
public function show(Product $product)
{
return view('products.show', compact('product'));
}
public function edit(Product $product)
{
return view('products.edit', compact('product'));
}
public function update(Request $request, Product $product)
{
$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.');
}
Step 4: Define Routes
In the routes/web.php
file, define the routes for the "Product" resource:
use App\Http\Controllers\ProductController;
Route::resource('products', ProductController::class);
Step 5: Create Views
Create views for the "index", "create", "edit", "show" pages in the resources/views/products
directory.
index.blade.php
@foreach($products as $product)
<p>{{ $product->name }} - {{ $product->price }}</p>
@endforeach
create.blade.php
<form method="POST" action="{{ route('products.store') }}">
@csrf
<input type="text" name="name" placeholder="Product Name">
<textarea name="description" placeholder="Product Description"></textarea>
<input type="number" step="0.01" name="price" placeholder="Product Price">
<button type="submit">Create</button>
</form>
edit.blade.php
<form method="POST" action="{{ route('products.update', $product->id) }}">
@csrf
@method('PUT')
<input type="text" name="name" value="{{ $product->name }}">
<textarea name="description">{{ $product->description }}</textarea>
<input type="number" step="0.01" name="price" value="{{ $product->price }}">
<button type="submit">Update</button>
</form>
show.blade.php
<h1>{{ $product->name }}</h1>
<p>{{ $product->description }}</p>
<p>Price: {{ $product->price }}</p>
Congratulations! You've successfully built a CRUD application with Laravel and MySQL. With this foundation, you can expand the application, add authentication, and incorporate more features to meet your specific needs.
Building CRUD applications is a crucial skill for web developers, and Laravel's ease of use and robust features make it an excellent framework for the job. Continue to explore Laravel's documentation to discover more advanced features and best practices.
Remember to optimize your code and database queries for better performance, and always prioritize security in your applications.