One to one relationship example in laravel

In database design, relationships are important to perform normalization. With a relationship we can easily store, retrieve and validate data. In 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. 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 table has a key that references another table's ID.

Here, We will create an Employee and Address table. Both tables will be connected to each other using one to one relationship. We will create model migration and relations for those tables.

Create New Laravel Application

Let's start with creating fresh Laravel application for this example. If you already have existing application then you can skip this step. As you know you can create new Laravel application with composer using one of the below commands. So open your terminal and enter below command :

    
        laravel new NAME
        //Or
        composer create-project --prefer-dist laravel/laravel NAME
    

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 other one is Address. Let's create one by one using terminal :

It will create Employee model and migration in our application. Here, -m flag is responsible for creating migration file for that model.

Let's define database schema for employee table. Open database/migrations/0000_00_00_000000_create_employees_table.php file and modify as below :

Here, we simply defined name, designation, date of birth and number fields to store information.

Let's create model and migration file for address table. Same as Employee open terminal and enter below command:

It will create model and migrations for addresses table. Let's modify addresses table and add address field and forign key for employees table. Open database/migrations/0000_00_00_000000_create_addresses_table.php file and modify as below:

Here we just added two columns. First one is Forign key for employee table and second column will be used to store address 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 employees and addresses table into 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 relationship in employee model, we will create new method address() in Employee model. Open app/Models/Employee.php and update as below:

In address method, we used hasOne and passed relation model class to it. Next, Eloquent automatically converts query to fetch data based on relation instance.

Now, we can store and retrieve address information from employee model but sometimes we require to get Inverse data. So let's define 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 inverse relationship.

Testing

To test relationship, let's store and retrieve data from both tables. For testing, we have created controller called HomeController. You can create new controller or use any existing controller.

Store Data into One to One Relationship

There are multiple ways to store data into models. We will discuss free ways in below code snippet.

Here, we have used save and create method to store data into both models. For both methods, first of all we will create employee model and then create and attach address 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 Eloquent relationship. You can use below code into any PHP class.

It will display following output :

Here, we find address based on employee. Let's find employee from address using inverse one to one relationship.

It will display following output :

You can use those 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 single collection object and you can use it as per your requirement.

Conclusion

Here, We have just created two models employee and addresses with one to one eloquent relationship. We have also performed example to store and retrieve data with relations in Laravel.


Share your thoughts

Ask anything about this examples