Eloquent ORM One-to-One Relationship MySQL — Laravel

Image for post
Image for post
Photo by Pixabay from Pexels

hen building a back-end, of course as developers we need a database to store the necessary data. The database needs are as important as the needs of the server itself, can you imagine a server without the database? There might be haha, but that is a rare case.

In creating a database, we need to create a relation between its tables. In general, relations between tables are divided into 3:

  • One-to-One
  • One-to-Many
  • Many-to-Many

In this article, I will show you one-to-one relationships and how to implement it in the Laravel project. Quick tips: It’s a very simple one :)

Content Overview

  • One-to-One Relationship
  • Laravel Preparations
  • Database Migrations
  • Eloquent ORM (Model)
  • Controllers
  • Restful API Routing

What’s One-to-One Relationship?

It is much easier to explain the one-to-one relationship with the following picture.

Image for post
Image for post
Image 1. ER Diagram One-to-One

As you can see, we have 2 tables here. The first table, the users table, is where we store our user data. The second table, the addresses table, we use it to store the user address data. Of course each user can only has 1 address, right? And each address can only be owned by one user, right? Yes, that’s why this relationship is called a one-to-one relationship.

One user can only have one address, and one address must be owned by one user. Of course we can find many examples that has one-to-one relationships like that.

If we look at the address table, there is one column named user_id in the form of a Foreign Key. The user_id column must have the same value as one of the id in the user table. Of course, because this relationship is called one-to-one, there shouldn’t be 2 addresses that have the same user_id.

Laravel Preparation

It’s the implementation time! Let’s create the Laravel project with the following command

composer create-project --prefer-dist laravel/laravel one-to-one

Now we have a new project named one-to-one. Navigate to the project and open it with any IDE, currently I use Visual Studio Code.

Database Migration

The first thing that needs to be done before storing any data into the database is, of course, creating the tables.

If you open app/database/migrations/, there is already a user_table migration. Let’s use the table instead for user table.

Image for post
Image for post

How about the address table? Let’s create the new one.

php artisan make:migration create_addresses_table

The convention for creating the migration name: start with a verb (create/add/anything) then continue with the table name (plural) and the table itself.

Let’s take a look inside the app/database/migrations. You will see a new file created

Image for post
Image for post

Let’s open and edit the create_addresses_table.php. For this project, we will use the img 1 database structure above. We need to add the zip_code, country, and user_id columns. The code will be like this.

app/database/migrations/create_addresses_table.php

The type of user_id column is bigInteger because the type must be the same as the id in the user table. Remember, this user_id column will be filled by the id in the user table.

In the last column, we create a foreign key for the user_id column, with having a reference id in the users table. onDelete (‘cascade’) means that when a user in the users table is deleted, the address related to that user will be deleted as well.

Before continuing, make sure your database is correct in the .env file and your XAMPP has already started.

The last thing we need to do is run the migration! Let Laravel do the rest for creating the tables by itself.

php artisan migrate

Eloquent ORM (Model)

Yeah we have the tables now, what next? For this project, I use Eloquent ORM (Model) of course. This Eloquent will make our lives easier as developers when storing/reading any data into database. Let’s create the model first.

php artisan make:model User
php artisan make:model Address

The models should appear in app/User.php and app/Address.php.

Let’s modify the User model first.

app/User.php

Then, open the Address model.

It’s done. Let’s review it. In the User model, we make a address() function and say it “Hey we have one address in App\Addresss, check it out!”. Make sense right? Same with the Address model, we have user() function and tell the Laravel “This data belongs to User in App\User”. Laravel will automatically match the user_id column in Address table and id column in User table.

Controller

Next, we have controllers! Let’s make them

php artisan make:controller UserController
php artisan make:controller AddressController

New files should appear in app/Http/Controllers/. Let’s modify the UserController first

app/Http/Controllers/UserController.php

Now open the AddressController

app/Http/Controllers/AddressController.php

There is nothing special in this code, but I created some new functions for validating the request data.

Restful API Routing

Phew! Last important step! Let’s create the API Routing. For now, because we don’t have any authorizations nor authentications, it’s a very simple routing. Take a look.

routes/api.php

It’s done! You can try the API or use my collection Postman API.

This is a very basic demo where you can find a one-to-one relationship between the user table and the address table. Go explore and modify the code, you will find that this is easy.

Postman API Collection: https://www.getpostman.com/collections/cdfe918d33f7be5fc591

Github:
https://github.com/Cerwyn/laravel-one-to-one

PHP/Backend Engineer at Undercurrent Capital Pte Ltd — Data Science Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store