Querying Data with Eloquent ORM

Tutorial 5 of 5

1. Introduction

Goal of the Tutorial

This tutorial aims to provide an in-depth understanding of how to query data using the Eloquent ORM (Object-Relational Mapping) in Laravel, a popular PHP framework.

What the User Will Learn

By the end of this tutorial, you will be able to:
- Understand what Eloquent ORM is and how it works
- Query data using various Eloquent methods
- Use Eloquent relationships to fetch related data

Prerequisites

To get the most out of this tutorial, you should have:
- Basic knowledge of PHP and Laravel
- Laravel installed on your local machine
- A database connected to your Laravel application

2. Step-by-Step Guide

Eloquent ORM provides a beautiful and simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" that is used to interact with that table.

Retrieving All Records

To begin with, let's see how to fetch all records from a table. For this example, let's assume we have a table named 'users'.

$users = User::all();

Here, User is the model corresponding to the 'users' table. The all() method retrieves all records from the table.

Retrieving Single Records

To retrieve a single record, you can use the find() method, passing the ID of the record you want to retrieve.

$user = User::find(1);

Filtering Records

Eloquent also makes it easy to apply conditions to your queries. For example, you can use the where() method to filter records.

$users = User::where('votes', '>', 100)->get();

Eloquent Relationships

Often, you'll need to access data from related tables. Eloquent makes this a breeze with its support for relationships. For example, if a 'User' has many 'Posts', you can retrieve all posts for a user like so:

$posts = User::find(1)->posts;

This assumes that you have defined a posts method on your User model that defines this relationship.

3. Code Examples

Now, let's put it all together with some practical examples.

Example 1: Retrieve all users who have more than 100 votes

$users = User::where('votes', '>', 100)->get();

// Output
foreach ($users as $user) {
    echo $user->name;
}

In this example, we are retrieving all users who have more than 100 votes. The get() method is used to execute the query and get the results.

Example 2: Retrieve all posts for a specific user

$posts = User::find(1)->posts;

// Output
foreach ($posts as $post) {
    echo $post->title;
}

In this example, we are retrieving all posts for the user with an ID of 1. This assumes that you have defined a posts method on your User model that defines the 'one-to-many' relationship.

4. Summary

In this tutorial, we've covered how to query data using Eloquent ORM in Laravel. We've learned how to retrieve all records, retrieve single records, filter records, and fetch related data.

To continue learning, you can explore more advanced Eloquent features such as eager loading, pagination, and more complex relationships.

5. Practice Exercises

  1. Exercise: Retrieve all users who have less than 50 votes.
    Solution:
    php $users = User::where('votes', '<', 50)->get();

  2. Exercise: Retrieve the user with the maximum votes.
    Solution:
    php $user = User::orderBy('votes', 'desc')->first();

  3. Exercise: Retrieve all posts for the user with an ID of 2.
    Solution:
    php $posts = User::find(2)->posts;

Keep practicing different types of queries and working with relationships to get a solid grasp of Eloquent ORM. Happy coding!