Make Todo List in Laravel 8 Tutorial

Todo List is software in the category of Task Management, Activity, Productivity, Motivation, and useful for completing goals.

Today, I’m going to show you how to make a simple Todo List app in Laravel 8 step by step. If you are a beginner, start making simple projects is a process for every programmer in order to gain more experience to solve any problem. once you are comfortable with it, move on to advance projects. without further ado, Let’s get started.

Creating a new Laravel Project

composer create-project laravel/laravel todolist

Making Controller, Model, Migration

php artisan make:model Todolist -mcr
create_todolists_table.php

Schema::create('todolists', function (Blueprint $table) {
            $table->id();
            $table->string('content');
            $table->timestamps();
});
php artisan migrate

Adding Routes

web.php

Route::get('/', [TodolistController::class, 'index'])->name('index');
Route::post('/', [TodolistController::class, 'store'])->name('store');
Route::delete('/{todolist:id}', [TodolistController::class, 'destroy'])->name('destroy');

Adding Controller actions

TodolistController.php

public function index()
{
    $todolists = Todolist::all();
    return view('home', compact('todolists'));
}

public function store(Request $request)
{
    $data = $request->validate([
        'content' => 'required'
    ]);

    Todolist::create($data);
    return back();
}


public function destroy(Todolist $todolist)
{
    $todolist->delete();
    return back();
}

Blade Template

home.blade.php

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Todo List</title>
    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" rel="stylesheet" />
    <!-- MDB -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.3.0/mdb.min.css" rel="stylesheet" />
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mdb-ui-kit/3.3.0/mdb.min.js"></script>
</head>

<body class="bg-info">
    <div class="container w-25 mt-5">
        <div class="card shadow-sm">
            <div class="card-body">
                <h3>To-do List</h3>
                <form action="{{ route('store') }}" method="POST" autocomplete="off">
                    @csrf
                    <div class="input-group">
                        <input type="text" name="content" class="form-control" placeholder="Add your new todo">
                        <button type="submit" class="btn btn-dark btn-sm px-4"><i class="fas fa-plus"></i></button>
                    </div>
                </form>
                {{-- if tasks count --}}
                @if (count($todolists))
                <ul class="list-group list-group-flush mt-3">
                    @foreach ($todolists as $todolist)
                    <li class="list-group-item">
                        <form action="{{ route('destroy', $todolist->id) }}" method="POST">
                            {{ $todolist->content }}
                            @csrf
                            @method('delete')
                            <button type="submit" class="btn btn-link btn-sm float-end"><i
                                    class="fas fa-trash"></i></button>
                        </form>
                    </li>
                    @endforeach
                </ul>
                @else
                <p class="text-center mt-3">No Tasks!</p>
                @endif
            </div>
            @if (count($todolists))
            <div class="card-footer">
                You have {{ count($todolists) }} pending tasks
            </div>
            @else

            @endif
        </div>
    </div>

</body>

</html>

Video Tutorial

598 Comments on “Make Todo List in Laravel 8 Tutorial”

  1. Pingback: acyclovir marone

Leave a Reply

Your email address will not be published. Required fields are marked *