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.
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.
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.
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.
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
php artisan tinker. Execute \App\Models\Task::count() to ensure your migration and seeding worked correctly.Task::all() logic in your TasksController as shown above.\App\Models\Task::create(['title' => 'Learn Laravel', 'is_completed' => false]); and refresh the page to see it appear instantly.use App\Models\Task; at the top of your controller is the most common cause of "Class not found" errors.php artisan migrate) and that your .env file points to a valid database file.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.MassAssignmentException when creating records in Tinker or controllers, remember to check your model's $fillable property.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.
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 moreLearn how to use Eloquent, Laravel's powerful ORM, to interact with database tables as clean, object-oriented models for your Task Manager application.
Task Manager: Displaying Real Database Records
Protecting Routes with Middleware
Understanding CSRF Protection
Preventing Mass Assignment
Task Manager: Securing the Application
Introduction to Route Model Binding
Updating Existing Records
Deleting Records
Using Named Routes
Task Manager: Completing CRUD Functionality
Introduction to Database Relationships
Querying Related Data
Handling File Uploads
Using Flash Messages for User Feedback
Task Manager: Adding Status and Priorities
Introduction to Artisan Commands
Debugging with Laravel Tinker
Understanding Service Providers
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation