រំលងទៅមាតិកា

កម្ពុជាត្រូវការសន្តិភាព / Cambodia needs peace

Laravel 4 នាទីអាន 1

Mastering Laravel: A Step-by-Step Guide to Building a Todo Application

Dive into the world of PHP development by building a functional Todo application with Laravel. This guide covers environment setup, database migrations, and CRUD operations to help you master the fundamentals.

Introduction to Laravel

Laravel is widely recognized as one of the most elegant and powerful PHP frameworks available today. Built on the principle of developer happiness, it provides a robust toolkit that simplifies common tasks such as routing, authentication, and database management. For beginners and experienced developers alike, building a Todo application is a rite of passage that offers a comprehensive look at the framework's core features. In this guide, we will walk through the process of creating a fully functional Todo app from scratch.

Prerequisites

Before we begin, ensure your development environment is ready. You will need:

  • PHP (version 8.1 or higher)
  • Composer (PHP package manager)
  • Node.js & NPM (for frontend assets)
  • A local database server (MySQL, PostgreSQL, or SQLite)

Step 1: Project Installation

First, open your terminal and run the following command to create a new Laravel project using Composer:

Bash
composer create-project laravel/laravel todo-app

Once the installation is complete, navigate into the project directory:

Bash
cd todo-app

Step 2: Database Configuration

Open the .env file in your project root. Configure your database connection details. For this tutorial, we will assume you are using MySQL:

ENV
DB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306DB_DATABASE=todo_dbDB_USERNAME=rootDB_PASSWORD=

Ensure you create the todo_db database in your MySQL server before proceeding.

Step 3: Creating the Model and Migration

In Laravel, models represent the data structure, and migrations handle the database schema. Use the Artisan CLI to generate a Model along with a migration and a controller in one command:

Bash
php artisan make:model Task -mc

Open the newly created migration file in database/migrations/xxxx_xx_xx_create_tasks_table.php and define the schema:

PHP
public function up(){    Schema::create('tasks', function (Blueprint $table) {        $table->id();        $table->string('title');        $table->boolean('is_completed')->default(false);        $table->timestamps();    });}

Run the migration to create the table:

Bash
php artisan migrate

Step 4: Defining Routes

Navigate to routes/web.php and define the routes for our application. We will need routes to view, create, update, and delete tasks:

PHP
use App\Http\Controllers\TaskController;use Illuminate\Support\Facades\Route;Route::get('/', [TaskController::class, 'index'])->name('tasks.index');Route::post('/tasks', [TaskController::class, 'store'])->name('tasks.store');Route::patch('/tasks/{task}', [TaskController::class, 'update'])->name('tasks.update');Route::delete('/tasks/{task}', [TaskController::class, 'destroy'])->name('tasks.destroy');

Step 5: Implementing Controller Logic

Open app/Http/Controllers/TaskController.php. Here, we will implement the CRUD logic:

PHP
namespace App\Http\Controllers;use App\Models\Task;use Illuminate\Http\Request;class TaskController extends Controller{    public function index() {        $tasks = Task::latest()->get();        return view('tasks', compact('tasks'));    }    public function store(Request $request) {        $request->validate(['title' => 'required|max:255']);        Task::create(['title' => $request->title]);        return back();    }    public function update(Task $task) {        $task->update(['is_completed' => !$task->is_completed]);        return back();    }    public function destroy(Task $task) {        $task->delete();        return back();    }}

Step 6: Creating the Frontend (Blade Template)

Create a file named tasks.blade.php inside resources/views/. This file will serve as the main interface. For styling, we will use a simple CDN-based CSS framework like Tailwind CSS.

HTML
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Laravel Todo App</title>    <script src="https://cdn.tailwindcss.com"></script></head><body class="bg-gray-100 p-10">    <div class="max-w-md mx-auto bg-white p-6 rounded shadow">        <h1 class="text-2xl font-bold mb-4">Todo List</h1>                <form action="{{ route('tasks.store') }}" method="POST" class="mb-4">            @csrf            <input type="text" name="title" class="border p-2 w-full" placeholder="New task...">            <button type="submit" class="bg-blue-500 text-white p-2 mt-2 w-full">Add Task</button>        </form>        <ul>            @foreach($tasks as $task)                <li class="flex justify-between items-center border-b py-2">                    <form action="{{ route('tasks.update', $task) }}" method="POST">                        @csrf @method('PATCH')                        <button class="{{ $task->is_completed ? 'line-through text-gray-400' : '' }}">                            {{ $task->title }}                        </button>                    </form>                    <form action="{{ route('tasks.destroy', $task) }}" method="POST">                        @csrf @method('DELETE')                        <button class="text-red-500">Delete</button>                    </form>                </li>            @endforeach        </ul>    </div></body></html>

Step 7: Testing the Application

Finally, start the Laravel development server:

Bash
php artisan serve

Visit http://127.0.0.1:8000 in your browser. You can now add tasks, mark them as completed by clicking on the text, and delete them using the delete button.

Conclusion

Congratulations! You have successfully built a Todo application using Laravel. Through this project, you have learned the fundamentals of the MVC (Model-View-Controller) architecture, database migrations, routing, and Blade templating. Laravel's expressive syntax makes these operations straightforward, allowing you to focus on building features rather than wrestling with boilerplate code. From here, you can extend this app by adding user authentication, categories, or due dates to further enhance your skills.

ចែករំលែកអត្ថបទនេះ

XLinkedIn

© 2026 Hel Mab. រក្សាសិទ្ធិគ្រប់យ៉ាង.

ភ្នំពេញ កម្ពុជា/បង្កើតដោយ Nuxt

ហ៊ែល ម៉ាប់

អ្នកអភិវឌ្ឍកម្មវិធីពេញលេញ