In database design, relationships are important to perform normalization. With a relationship, we can easily store, retrieve and validate data. In the Laravel framework, Eloquent provides an easy way to work with database relationships.
In this example, we create two different tables and define the relationship between them. A one-to-one relationship is a very simple and basic relationship compared to others.
To define one to one relationship you just need to make sure that one of the tables has a key that references another table's ID.
Here, We will create an Employee and Address table. Both tables will be connected using one to one relationship. We will create model migration and relations for those tables.
Create New Laravel Application
Let's start with creating a fresh Laravel application for this example. If you already have an existing application then you can skip this step. As you know you can create a new Laravel application with composer using one of the below commands. So open your terminal and enter below command:
Database Configuration
Before starting let's configure our database. Open .env file from root directory and update below value as per your configuration:
Creating Models and Migrations
Here, we have to create two models for this example one is an Employee and second is an Address. Let's create them one by one using the terminal:
It will create an Employee model and migration in our application. Here, -m flag is responsible for creating a migration file for that model.
Let's define the database schema for employee table. Open employee migration file and modify it as below:
Let's create a model and migration file for the address table. Same as Employee open terminal and enter below command:
Here we just added two columns. The first one is Foreign key for employee table and second column will be used to store addresses in text data.
Migrate Database
Now, let's migrate employees and addresses to our database. Open terminal and enter below command to migrate:
It will create an employees and addresses table in your database.
Defining One to One Relationship In Laravel
Let's modify model and define one to one relation between Employee and Address model. To define a relationship in employee model, we will create a new method address() in Employee model. Open app/Models/Employee.php and update as below:
In the address method, we used hasOne() and passed relation model class to it. Next, Eloquent automatically converts queries to fetch data based on relation instances.
Now, we can store and retrieve address information from employee model but sometimes we require to get Inverse data. So let's define the Inverse relation between both tables. Open app/Models/Address.php and update as below:
Same as Employee we have defined employee method in address model to get employee information based on one to one relationship with address table. We have used belongsTo() method to define an inverse relationship.
Testing
To test a relationship, let's store and retrieve data from both tables. For testing, we have created a controller called HomeController. You can create a new controller or use any existing controller.
Store Data in One-to-One Relationship
There are multiple ways to store data in models. We will discuss freeways in below code snippet.
Here, we have used save and create a method to store data in both models. For both methods, first of all, we will create employee model and then create and attach addresses to employee using one to one relationship.
Retrieve Data into One to One Relationship
In below code snippet, we will see how to retrieve data from both models using the Eloquent relationship. You can use below code in any PHP class.
It will display following output :
Here, we find addresses based on employee. Let's find employee from address using an inverse one-to-one relationship.
It will display following output :
You can use that relation instance to eager loading too. Please consider below example to load data from both models using relation.
It will automatically load both models data into a single collection object and you can use it as per your requirement.
Conclusion
Here, We have just created two models employee and address with one-to-one eloquent relationship. We have also performed examples to store and retrieve data with relations in Laravel.