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.
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.
Generating Your First Eloquent Model
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.
Associating Models with Tables
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.
Retrieving Records via Eloquent
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.
Hands-on Exercise
To solidify your understanding, let's interact with our Task model:
- Open your terminal and run
php artisan tinker. This opens an interactive shell where you can execute Laravel code in real-time. - Inside the tinker shell, type
\App\Models\Task::all();. You should see an empty collection[](assuming your table is empty). - Try to fetch a record by ID:
\App\Models\Task::find(1);. - Exit tinker by typing
exit.
Common Pitfalls
- Namespace Mismatches: Always ensure your models are in the
App\Modelsnamespace. If you move a file, remember to update the namespace declaration at the top of the file. - The "N+1" Problem: When loading related data (which we will cover in Laravel Eloquent Relationships: A Guide to Linking Data Models), beginners often trigger too many database queries. We will address this later with "eager loading."
- Ignoring Conventions: While you can name your tables and models anything, life is much easier if you follow Laravel's naming conventions (singular model, plural table).
Recap
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.
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

FilamentPHP Admin Panel & Dashboard Development
A powerful admin panel for your Laravel app — built with FilamentPHP so you can manage everything without touching the database.