Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 20 of the Laravel Fundamentals: From Zero to Your First App course
LaravelJune 25, 20263 min read

Task Manager: Displaying Real Database Records

Learn how to display database data in your Laravel Task Manager. We'll connect your Eloquent models to your Blade views to render real, dynamic tasks.

LaravelEloquentDatabaseMVCTask Managerphpbackend

Previously in this course, we covered Task Manager: Building the User Interface with Blade using hardcoded arrays. Now, it's time to stop faking it. We are moving from static mockups to a dynamic application by querying our database directly.

To build a professional application, your views must reflect the state of your database. In this lesson, we’ll replace those hardcoded variables in your controller with real data fetched via Eloquent, bringing our Task Manager to life.

Fetching Data with Eloquent

In Laravel, the controller acts as the bridge between your database and your view. Instead of defining a $tasks array manually, we will use our Task model—which we explored in our guide to Performing Basic CRUD Operations in Laravel with Eloquent—to retrieve records from the tasks table.

Eloquent models provide a clean, object-oriented syntax for database queries. When you call Task::all(), Laravel executes a SELECT * FROM tasks query and returns a collection of model instances.

Worked Example: Updating the TasksController

Open your app/Http/Controllers/TasksController.php. We need to import the Task model and update the index method to fetch records from the database instead of using a local variable.

PHP
<?php

namespace App\Http\Controllers;

use App\Models\Task; #6A9955">// 1. Import the model
use Illuminate\View\View;

class TasksController extends Controller
{
    public function index(): View
    {
        #6A9955">// 2. Fetch all tasks from the database
        $tasks = Task::all();

        #6A9955">// 3. Pass the collection to the view
        return view('tasks.index', ['tasks' => $tasks]);
    }
}

By passing $tasks to the view, the collection becomes available as an $tasks variable inside your Blade template. Because this is an Eloquent collection, you can iterate over it exactly as you did with your mock array in the previous lesson.

Rendering Records in the View

With the controller updated, your view should now automatically display whatever is in your database. If you have followed the previous steps for seeding the database, your table should already contain records.

Open your resources/views/tasks/index.blade.php. If you used an @forelse directive, no changes are required to the template logic itself, but ensure your variable naming remains consistent:

BLADE
@extends('layouts.app')

@section('content')
    <h1>My Tasks</h1>

    <ul>
        @forelse ($tasks as $task)
            <li>{{ $task->title }} - {{ $task->is_completed ? 'Done' : 'Pending' }}</li>
        @empty
            <li>No tasks found.</li>
        @endforelse
    </ul>
@endsection

Hands-on Exercise

  1. Verify your database: Open your terminal and run php artisan tinker. Execute \App\Models\Task::count() to ensure your migration and seeding worked correctly.
  2. Update the controller: Implement the Task::all() logic in your TasksController as shown above.
  3. Refresh your browser: Navigate to your task list route. You should see the records you previously seeded.
  4. Add a record: While in Tinker, run \App\Models\Task::create(['title' => 'Learn Laravel', 'is_completed' => false]); and refresh the page to see it appear instantly.

Common Pitfalls

  • Missing Imports: Forgetting to add use App\Models\Task; at the top of your controller is the most common cause of "Class not found" errors.
  • Database Connection: If you see a "Table not found" error, ensure you have run your migrations (php artisan migrate) and that your .env file points to a valid database file.
  • The N+1 Problem: While Task::all() is fine for small lists, as your app grows, you might need to eager load relationships. We will cover this in detail when we discuss database relationships, but keep an eye on your query logs.
  • Mass Assignment: If you get a MassAssignmentException when creating records in Tinker or controllers, remember to check your model's $fillable property.

Recap

We have successfully transitioned our Task Manager from static mock data to a dynamic, database-driven application. By importing our Eloquent model into the TasksController and passing the results to our Blade view, we've created a robust foundation for the rest of our CRUD operations. You now have a working UI that reflects real-time database state.

Up next: We will learn how to accept user input by building our first HTML form to create new tasks dynamically.

Previous lessonSeeding the DatabaseNext lesson Capturing User Input from Forms
Back to Blog

Similar Posts

LaravelJune 25, 20263 min read

Performing Basic CRUD Operations in Laravel with Eloquent

Master database operations with our guide to CRUD in Laravel. Learn how to save, fetch, update, and delete records using the powerful Eloquent ORM.

Read more
LaravelJune 25, 20263 min read

Working with Eloquent Models in Laravel: A Beginner's Guide

Learn how to use Eloquent, Laravel's powerful ORM, to interact with database tables as clean, object-oriented models for your Task Manager application.

Part of the course

Laravel Fundamentals: From Zero to Your First App

beginner · Lesson 20 of 52

  1. 1

    Setting Up the Local Development Environment

    4 min
  2. 2

    Installing Laravel and Exploring Directory Structure

    3 min
  3. 3

    Understanding the .env File and Configuration

    3 min
Read more
LaravelJune 25, 20263 min read

Understanding Database Migrations: A Laravel Beginner's Guide

Learn how to manage your database schema using Laravel migrations. Discover how to create, define, and run schema updates for your Task Manager project.

Read more
  • 4

    The Laravel Application Lifecycle

    4 min
  • 5

    Initializing the Task Manager Project

    3 min
  • 6

    Defining Basic Web Routes

    4 min
  • 7

    Using Route Parameters

    3 min
  • 8

    Creating Your First Controller

    3 min
  • 9

    Returning Responses and Redirects

    3 min
  • 10

    Task Manager: Implementing the Task List Route

    3 min
  • 11

    Introduction to Blade Templating

    3 min
  • 12

    Using Blade Layouts and Sections

    3 min
  • 13

    Implementing Blade Partials

    4 min
  • 14

    Mastering Blade Directives for Loops and Conditionals

    3 min
  • 15

    Task Manager: Building the User Interface

    3 min
  • 16

    Understanding Database Migrations

    3 min
  • 17

    Working with Eloquent Models

    3 min
  • 18

    Performing Basic CRUD Operations

    3 min
  • 19

    Seeding the Database

    3 min
  • 20

    Task Manager: Displaying Real Database Records

    3 min
  • 21

    Capturing User Input from Forms

    4 min
  • 22

    Introduction to Laravel Validation

    3 min
  • 23

    Customizing Validation Error Messages

    3 min
  • 24

    Using Form Requests for Validation

    3 min
  • 25

    Introduction to Authentication

    4 min
  • 26

    Protecting Routes with Middleware

    Coming soon
  • 27

    Understanding CSRF Protection

    Coming soon
  • 28

    Preventing Mass Assignment

    Coming soon
  • 29

    Task Manager: Securing the Application

    Coming soon
  • 30

    Introduction to Route Model Binding

    Coming soon
  • 31

    Updating Existing Records

    Coming soon
  • 32

    Deleting Records

    Coming soon
  • 33

    Using Named Routes

    Coming soon
  • 34

    Task Manager: Completing CRUD Functionality

    Coming soon
  • 35

    Introduction to Database Relationships

    Coming soon
  • 36

    Querying Related Data

    Coming soon
  • 37

    Handling File Uploads

    Coming soon
  • 38

    Using Flash Messages for User Feedback

    Coming soon
  • 39

    Task Manager: Adding Status and Priorities

    Coming soon
  • 40

    Introduction to Artisan Commands

    Coming soon
  • 41

    Debugging with Laravel Tinker

    Coming soon
  • 42

    Understanding Service Providers

    Coming soon
  • 43

    Using View Composers

    Coming soon
  • 44

    Task Manager: Refactoring for Clean Code

    Coming soon
  • 45

    Introduction to Testing

    Coming soon
  • 46

    Testing Forms and Validation

    Coming soon
  • 47

    Using Database Transactions

    Coming soon
  • 48

    Handling Global Exceptions

    Coming soon
  • 49

    Preparing for Production

    Coming soon
  • 50

    Environment Security Best Practices

    Coming soon
  • 51

    Managing Assets in Production

    Coming soon
  • 52

    Task Manager: Deployment Preparation

    Coming soon
  • View full course