Laravel 9 One To One | HasOne Eloquent Relationship Tutorial
https://www.laravelia.com/post/laravel-one-to-onehasone-eloquent-relationship-tutorial
In a relational database management system, you know that database tables are often related to one another. For example, a company may have many employees or an order could be related to the user who placed it. Laravel eloquent makes managing and working with these relationships very convenient. Laravel provides many relationships and in this tutorial, we will see how we handle one to one or we can call this hasOne eloquent relationship.
In this Laravel 9 one to one relationship example tutorial, I am going to use the User and Phone concept. Suppose, A user may have one phone and a phone may have one user. Let's see how we can define Laravel's one to one relationship.
Eloquent relationships are defined as methods in your Eloquent model classes. We can use the hasOne() method to define one to one relationships and the first argument passed to the hasOne method is the name of the related model class.

C:\xampp\htdocs\wayarmy\routes\web.php
C:\xampp\htdocs\wayarmy\database\factories\UserFactory.php
C:\xampp\htdocs\wayarmy\database\factories\PhoneFactory.php
C:\xampp\htdocs\wayarmy\database\seeders\DatabaseSeeder.php
C:\xampp\htdocs\wayarmy\app\Models\User.php
C:\xampp\htdocs\wayarmy\app\Models\Phone.php
C:\xampp\htdocs\wayarmy\app\Http\Controllers\TestController.php
C:\xampp\htdocs\wayarmy\database\migrations\2022_11_17_154429_create_phones_table.php
C:\xampp\htdocs\wayarmy\database\migrations\2014_10_12_000000_create_users_table.php
In a relational database management system, you know that database tables are often related to one another. For example, a company may have many employees or an order could be related to the user who placed it. Laravel eloquent makes managing and working with these relationships very convenient. Laravel provides many relationships and in this tutorial, we will see how we handle one to one or we can call this hasOne eloquent relationship.
In this Laravel 9 one to one relationship example tutorial, I am going to use the User and Phone concept. Suppose, A user may have one phone and a phone may have one user. Let's see how we can define Laravel's one to one relationship.
Eloquent relationships are defined as methods in your Eloquent model classes. We can use the hasOne() method to define one to one relationships and the first argument passed to the hasOne method is the name of the related model class.

Assume that one user may have one Phone. So we can define this relationship like this:
app/Models/User.php
PHPCopy
Now we can fetch every user's phone information from the controller like:
App\Http\Controllers\TestController.php
PHPCopy
Defining Inverse Relationship of hasOne
Now we know how to define hasOne or one to one relationship. Now we will see how we can define the inverse relationship of hasOne. Let's define a relationship on the Phone model that will let us access the user that owns his/her phone. We can define the inverse of a hasOne relationship using the belongsTo method like:
app/Models/Phone.php
PHPCopy
Read also: Laravel 9 Throttle Rate Limiters Middleware Example
Here we can also pass fereign_key and local_key like below:
PHPCopy
Now we can also fetch user like:
App\Http\Controllers\TestController.php
Last updated
Was this helpful?