Learn how to use Eloquent, Laravel's powerful ORM, to interact with database tables as clean, object-oriented models for your Task Manager application.
Previously in this course, we covered understanding database migrations, where we defined the structure of our tasks table. Now that we have a schema, we need a way to interact with that data without writing complex SQL queries.
Enter Eloquent.
Eloquent is Laravel's Object-Relational Mapper (ORM). It allows you to treat your database tables as PHP classes (Models). Instead of writing SELECT * FROM tasks, you’ll interact with a Task object. This makes your code more readable, maintainable, and significantly faster to write.
In Laravel, every model typically corresponds to a database table. By convention, if your table is named tasks, your model should be named Task.
To generate a model, use the Artisan command-line tool. Open your terminal in your project root and run:
Bashphp artisan make:model Task
This command creates a new file at app/Models/Task.php. Open it, and you'll see a simple class extending Illuminate\Database\Eloquent\Model. This single line of inheritance gives your class access to dozens of powerful methods for querying, inserting, and deleting data.
Laravel is smart. Because we named our model Task, Eloquent automatically assumes it belongs to a table named tasks. This "convention over configuration" approach saves you from having to define metadata for every single table.
If you ever need to deviate from this naming convention, you can explicitly define the table name inside the class:
PHPnamespace App\Models; use Illuminate\Database\Eloquent\Model; class Task extends Model { #6A9955">// Only needed if your table is NOT named 'tasks' protected $table = 'my_custom_task_table'; }
For our Task Manager project, the default convention works perfectly. You don't need to add anything else to the class for now.
Once your model is linked to a table, you can start fetching data. Eloquent provides a fluent, expressive syntax that reads almost like English.
Let’s look at how we retrieve data in a controller. If you wanted to fetch all tasks from your database, you would use the all() method:
PHPuse App\Models\Task; $tasks = Task::all();
This returns a Collection of Task objects. If you only wanted to find a specific task by its ID, you would use find():
PHP$task = Task::find(1);
If you need to filter your results, you can chain query methods before calling get():
PHP#6A9955">// Get all tasks that are marked as 'completed' $completedTasks = Task::where('status', 'completed')->get();
The power of Eloquent is that it abstracts away the database driver. Whether you are using SQLite, MySQL, or PostgreSQL, your code remains exactly the same.
To solidify your understanding, let's interact with our Task model:
php artisan tinker. This opens an interactive shell where you can execute Laravel code in real-time.\App\Models\Task::all();. You should see an empty collection [] (assuming your table is empty).\App\Models\Task::find(1);.exit.App\Models namespace. If you move a file, remember to update the namespace declaration at the top of the file.We've successfully moved from raw SQL thinking to an object-oriented approach. By generating a Task model, we’ve bridged the gap between our database schema and our application logic. Eloquent models are the backbone of your application, providing the tools you need to manage your data effortlessly.
Understanding these Eloquent basics: models, relationships, and your first queries is the single biggest step toward becoming a proficient Laravel developer.
Up next: Performing Basic CRUD Operations.
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.
Read moreMaster database operations with our guide to CRUD in Laravel. Learn how to save, fetch, update, and delete records using the powerful Eloquent ORM.
Working with Eloquent Models
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