
Laravel Crud
The CRUD (Create, Read, Update, Delete) operations are fundamental in web development, and Laravel makes it easy to implement them. Below are the steps to create a basic CRUD system in a Laravel application:
Step 1: Create a New Laravel Project
If you haven’t already, create a new Laravel project using Composer:
composer create-project laravel/laravel laravel-crud
Step 2: Set Up the Database
Configure your database connection by modifying the .env
file with your database credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password
Then, run the migrations to create the necessary tables:
php artisan migrate
Step 3: Create a Model
Create a model for the resource you want to perform CRUD operations on. For example, if you’re managing a “Task” resource:
php artisan make:model Task -m
This will generate a Task
model and a migration file.
Step 4: Define the Migration
In the generated migration file (e.g., database/migrations/xxxx_xx_xx_create_tasks_table.php
), define the table schema:
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('description');
$table->timestamps();
});
}
Run the migration to create the “tasks” table:
php artisan migrate
Step 5: Create Routes
Define routes for your CRUD operations in routes/web.php
. Here’s an example:
Route::resource('tasks', 'TaskController');
This single line of code generates routes for all the CRUD operations: create, read, update, and delete.
Step 6: Create a Controller
Generate a controller to handle CRUD operations:
php artisan make:controller TaskController
In your TaskController.php
, add methods to perform CRUD operations:
use App\Task;
public function index()
{
$tasks = Task::all();
return view('tasks.index', compact('tasks'));
}
public function create()
{
return view('tasks.create');
}
public function store(Request $request)
{
Task::create($request->all());
return redirect()->route('tasks.index')->with('success', 'Task created successfully.');
}
public function show(Task $task)
{
return view('tasks.show', compact('task'));
}
public function edit(Task $task)
{
return view('tasks.edit', compact('task'));
}
public function update(Request $request, Task $task)
{
$task->update($request->all());
return redirect()->route('tasks.index')->with('success', 'Task updated successfully.');
}
public function destroy(Task $task)
{
$task->delete();
return redirect()->route('tasks.index')->with('success', 'Task deleted successfully.');
}
Step 7: Create Views
Create Blade views for listing, creating, updating, and displaying individual tasks. Place these views in the resources/views/tasks
directory.
Here are some example views:
index.blade.php
: List all tasks.create.blade.php
: Create a new task.edit.blade.php
: Edit an existing task.show.blade.php
: Display an individual task.
Step 8: Use Forms in Views
In your create and edit views, use Laravel’s Form
class or plain HTML forms to create and update tasks. For example:
<form method="POST" action="{{ route('tasks.store') }}">
@csrf
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" class="form-control">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea name="description" class="form-control"></textarea>
</div>
<button type="submit" class="btn btn-primary">Create Task</button>
</form>
Step 9: Display Flash Messages
To provide user feedback for CRUD operations, use session flash messages. For example, in your view:
@if(session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
Step 10: Test Your CRUD Operations
You can now run your Laravel application and navigate to the routes you defined to test the CRUD operations.
For example:
- Create:
/tasks/create
- Edit:
/tasks/{task}/edit
- Show:
/tasks/{task}
- Delete: No specific route, but you can add a delete button in your views.
That’s it! You’ve created a basic CRUD system in Laravel. You can further enhance it with validation, authentication, and more features to suit your application’s needs.