Laravel One To One Relationship Example

Laravel one to one relationship example is explained here.

As you might know that laravel uses eloquent ORM model to maintain relation between two or more tables of the database.

Relation between database records have certain types like :

  • One to One
  • One to Many
  • Many to Many etc.

All the types has different concept as their name suggests.

Today, I will teach you about one to one relationship of laravel in detail.

One to One Eloquent Relationship

Laravel eloquent one to one relationship is based on the relation between two records in the database.

It primary tells that one row of one table has relation with only one row of another table.

For example, we have two tables : Users and Bikes

Now one user must have maximum of only one bike but it may happen that any user has no bikes. So user can have zero or one bike.

User table will have rows which contains the id and name of the user. Bike table have id, user_id and name of the bike.

Here, user_id from Bikes table represents the id column from the Users table.

So, if the name of the user is Hardik and id is 1 then in the Bikes table, user_id will be 1 and a bike name will be associated with this row.

See the below image to understand this whole scenario.

Laravel one to one relationship

Two Tables for one to one


Here is the official documentation about one to one relationship.

Let us create a new practical laravel example which uses one to one relationship concept.

Check the below video which is the result of this Laravel one to one relationship example.

Step 1. Project and Database Works

Trigger the below command first,

It will simply make a new laravel project.

Now go to your database administration tool. I will use sequel pro but if you are using something else like phpMyAdmin then it is also ok.

Process of database will be same for all king of database administration tool.

Make a new database in your tool and give it a name like “laraonetoone

Now to connect this database with your laravel project, you need to update .env file of your project.

So open your editor and navigate to .env file. See the below lines

You need to modify these lines as the configuration of your database credentials.

My username is root and password is empty.

After this, you have successfully connected your database and laravel project.

Step 2. Making Two Tables in Database

Let us create two tables in the database to establish one to one relationship between them.

In your terminal, run the below command

This command will make a new migration file. You can navigate to this file via timestamp_create_bikes_table.php file.

Write down the below source lines in this migration file.

Look at the up() function in above file.

It contains the column names which Bikes table will have. Column are id, user_id, bike_name and timestamps.

Now run the following command

Above command will create some tables in our “laraonetoone” database.

We will work with two tables : users and bikes

Step 3. Working with Models

Open the app->User.php file. You will see that there are some functions are already there.

You need to add below function in this file.

This functions contains the hasOne() method. This method says that users table has relation with one row in the bikes table.

This method will enable us to fetch row from bikes table.

So, final code for User.php file is as the below

Bike Model

Now we need to create another model called “Bike.php

For that, run the below command in terminal

It will create a model in app->Bike.php directory.

Inside this new model, add the below source writings

First line will help us to perform some eloquent tasks on bikes tables using this model class.

Inverse One to One Relationship

Then, there is user() function.

This function uses belongsTo() method. This method says that any row from bikes table is connected with any one row of users table.

So this belongsTo() method is the inverse of hasOne() method or this is the concept of inverse one to one relation in laravel.

Step 4. Add Records in Database

Now we need to add some records in our database manually.

Add four rows in users table and two rows in bikes table.

After adding records in users and bikes table, your both the tables should look like the below image.

Laravel one to one relationship

Two Tables for one to one


Now we will fetch the records from these tables using one to one relationship concept.

Step 5. Route and Controller Task

Open up your app->routes->web.php file.

We will add only one route line in this file. Final source snippet for this file is as the below

I have added last line manually. Other things are pre written.

When you open in your browser, compiler will call usersWithBike() function from BikesController.php file.

We do not have created BikesController.php file, so let’s do this first.

Trigger the below command first,

After this, you will find a BikesController.php file inside app->Http->Controllers directory.

Add the below source code snippet in this controller file.

  • Now usersWithBike() method is the heart of this example.
  • It’s first line will get the row from bikes table. (System will try to fetch the row from bikes table where user_id is equals to which is the row of BMW)
  • So you can see that we have fetched the row from bikes table using User model, this is the practical example of one to one relationship.
  • Similarly, second line will fetch the row from bikes table where user_id is 3.
  • Now, third line is little different. It will fetch the row from users table where id is 1. Here, we are using Bike model to get row from users table, so this is the inverse of one to one relationship.
  • And finally, fourth line will get the row from users table where id is 3.
  • Finally, we have seen that user_id (bikes table) has one to one relationship with id (users table)

Now consider the following lines

First line is fetching all the records from users table.

Second line will insert all the rows from bikes table into the $allbikes

See the below return statement.

At last, compiler will return the index.blade.php file. We will create this view file in the next step.

Along with view file, compiler will send variables like users, allbikes etc. to this view file so that we can retrieve values from them.

Step 6. Last View file

Let us create index.blade.php file.

Under resources->views directory, make a new file and give it a name like index.blade.php

Write the below source code structure in this file.

We have passed several variables in the previous step in this file. (see the return statement in step 5)

Here, we are using these variables to get required fields like bike name and user name at required places.

Download Code For Laravel One To One Relationship

Click here for whole code of this example

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *