Building a Task List with Priority Levels in Laravel
Introduction
In this tutorial, we’ll create a task list application using Laravel, where each task can be assigned a priority level. This feature is helpful for users to organize tasks based on urgency or importance. We'll explore how to set up the backend, define priority levels, and render tasks with color-coded priorities.
Step 1: Set Up Laravel Project
Create a new Laravel project and set up a database.
composer create-project laravel/laravel task-list cd task-list php artisan migrate
Step 2: Create the Task Model and Migration
Generate a model and migration for the Task.
php artisan make:model Task -m
In the migration file, define fields for the task title, description, and priority.
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('description')->nullable();
$table->enum('priority', ['low', 'medium', 'high']);
$table->timestamps();
});Run the migration.
php artisan migrate
Step 3: Implement Task Management Logic
In TaskController.php, define methods for creating, editing, and deleting tasks. Include logic to sort tasks by priority.
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|max:255',
'description' => 'nullable',
'priority' => 'required|in:low,medium,high',
]);
Task::create($validated);
return redirect()->route('tasks.index');
}Step 4: Create Views with Priority Labels
In resources/views, create a Blade template to list tasks with priority labels. Use Bootstrap classes for color-coding.
@foreach($tasks as $task)
<div class="task-item {{ $task->priority }}">
<h5>{{ $task->title }}</h5>
<p>{{ $task->description }}</p>
<span class="badge bg-{{ $task->priority }}">{{ ucfirst($task->priority) }}</span>
</div>
@endforeachStep 5: Test the Application
Run the server and navigate to the task list page to test adding and viewing tasks with priority levels.
php artisan serve
Conclusion
This simple application lets users manage tasks with priority levels, offering an organized way to handle tasks based on urgency. You can extend this application by adding filters or notifications for high-priority tasks.
Comments (0)
Leave a Comment
You need to login to post a comment
Login to Comment