Tutorial series

A Practical Introduction to Laravel Eloquent ORM

A Practical Introduction to Laravel Eloquent ORM
<- Back to all series

Author(s)

Erika Heidi

By Erika Heidi

Developer Advocate

Introduction

Introduction

Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. An ORM is software that facilitates handling database records by representing data as objects, working as a layer of abstraction on top of the database engine used to store an application’s data.

Eloquent facilitates the task of interacting with database tables, providing an object-oriented approach to inserting, updating, and deleting database records, while also providing a streamlined interface for executing complex SQL queries.

In this project-based series, you’ll learn how to make database queries and how to work with relationships in Laravel Eloquent. To practice the examples explained throughout the series, you’ll download a demo Laravel application and use the included Docker Compose setup to run a PHP development environment on containers. The demo consists of a single page application that shows a list of links or bookmarks. You’ll improve the application by adding new models and defining new relationships between them that will extend the current database structure.

If you’d like an introduction to the Laravel framework, you can follow the guide on How To Build a Links Landing Page in Laravel. That tutorial explains how you can build the demo application that this series uses as a foundation from scratch.

Prerequisites

Although the code shared throughout this series should work seamlessly across multiple environments and systems, the instructions explained here were tested within an Ubuntu 20.04 local system running Docker and Docker Compose. Regardless of your base operating system, here’s what you’ll need to set up in order to get started:

  • Docker installed on your local machine or remote development server. If you’re running Ubuntu 20.04, you can follow Steps 1 and 2 of How To Install and Use Docker on Ubuntu 20.04 to set it up. Windows and MacOS users need to install Docker Desktop instead.
  • Docker Compose installed on your local machine or development server. Docker Compose comes included by default with Docker Desktop for both Windows and MacOS systems, but Linux users need to install the Compose executable, following Step 1 of How To Install and Use Docker Compose on Ubuntu 20.04.
  • A code editor for PHP (optional). A code editor helps making code easier to read and to format, and can improve your productivity by pointing out issues before you execute your code. You can follow our guide on How To Set Up Visual Studio Code for PHP Projects to set up VSCode, a free code editor, within your local development environment.

Setting Up the Demo Project

To get started, you’ll need to set up the Landing Laravel demo project. There are two ways in which you can get the demo application code ready to use with this series:

  1. The first way is to follow the guide on How To Build a Links Landing Page in Laravel. That series explains how to build the demo application that you’ll use as base for the current series from scratch. If you choose this option, you can move on to the first tutorial in this series How To Create a One-To-Many Relationship in Laravel Eloquent.

  2. The second option is to download the complete demo application code and use it as the base that you will build on in this series. On the application releases page, you’ll find separate application versions for each tutorial in the series. You can choose to start from the first tutorial by downloading version 0.1.1, or you can choose to download one of the elo-tutorial releases that are paired with each individual tutorial in the series.

To download the code from GitHub, first make sure that you have the curl and unzip tools available on your system:

  1. sudo apt update
  2. sudo apt install curl unzip

Next, change to your home directory using the cd ~ command, and then download your preferred project release using curl. To start from the first guide, you can download the 0.1.1 release:

  1. cd ~
  2. curl -L https://github.com/do-community/landing-laravel/archive/refs/tags/0.1.1.zip -o landing-laravel.zip

The demo code will be saved in the landing-laravel.zip file. Unzip the contents of the zip file, rename the directory to landing-laravel, and cd into the application folder using the following commands:

  1. unzip landing-laravel.zip
  2. mv landing-laravel-0.1.1 landing-laravel
  3. cd landing-laravel

The MySQL image that you will use with Docker Compose uses environment variables to set up the database user and password. Before bringing the environment up for the first time, you’ll need to set up an .env file that contains database credentials. The demo application includes an example .env.example file that you can use. Copy it in place by running the following command:

  1. cp .env.example .env

You can now bring your environment up with docker-compose:

  1. docker-compose up -d

Install the Laravel application dependencies with:

  1. docker-compose exec app composer install

This will install the PHP dependencies that Laravel needs via Composer. Next, create a unique application key using artisan:

  1. docker-compose exec app php artisan key:generate

You should receive output like the following, indicating that artisan added a unique application key to your .env file:

Output
Application key set successfully.

Finally, run the database migrations and seeders to set up the database:

  1. docker-compose exec app php artisan migrate --seed

You will receive output like the following, which indicates the database tables, and seed data are configured correctly:

Output
Migration table created successfully. . . . Database seeding completed successfully.

The application is now configured and running in your development environment. Point your browser to http://localhost:8000 to access it. If you’re starting at the first guide of the series, you’ll receive a page similar to this:

Landing Laravel demo application

With this base structure in place, you can proceed with the rest of the tutorials in this series.

Tutorials in series

Tutorial

How To Create a One-To-Many Relationship in Laravel Eloquent

Tutorial

How To Insert New Database Records in Laravel Eloquent

Tutorial

How To Populate a Database with Sample Data using Laravel Seeders and Eloquent Models

Tutorial

How To Query the Database in Laravel with Eloquent (Select)

Tutorial

How To Refine Database Queries in Laravel with Eloquent Where()

Tutorial

How To Order Query Results in Laravel Eloquent

Tutorial

How To Get Total Result Count in Laravel Eloquent

Tutorial

How To Limit and Paginate Query Results in Laravel Eloquent

Tutorial

How To Update Database Records in Laravel Eloquent

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.