By Erika Heidi
Developer Advocate
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.
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:
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:
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.
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:
- sudo apt update
- 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:
- cd ~
- 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:
- unzip landing-laravel.zip
- mv landing-laravel-0.1.1 landing-laravel
- 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:
- cp .env.example .env
You can now bring your environment up with docker-compose
:
- docker-compose up -d
Install the Laravel application dependencies with:
- 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
:
- 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:
OutputApplication key set successfully.
Finally, run the database migrations and seeders to set up the database:
- 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:
OutputMigration 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:
With this base structure in place, you can proceed with the rest of the tutorials in this series.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.