
In Laravel, we use a One-to-One relationship when one record in a table corresponds to exactly one record in another table. This type of relationship is helpful when we need to store related data in separate tables for better organization and clarity.
Example Scenario:
Consider a User table and a National ID Card table. Each user has one unique national ID card, and each national ID card is linked to one specific user. In this case, a One-to-One relationship is perfect because of the direct connection between the user and their national ID card.
Setting Up a One-to-One Relationship in Laravel
To set up this relationship, you need to define it both in the database and in the models. Here’s a step-by-step guide for the example of User and National ID Card.
Step 1: Create Migration Files for the Tables
Users Table
- Run the following command to create a migration for the users table:
- php artisan make:migration create_users_table
National ID Card Table
- Run the following command to create a migration for the national_id_cards table:
- php artisan make:migration create_national_id_cards_table
Step 2: Define Fields in the Migration Files
Users Table Migration
- In the create_users_table migration file, add fields like id, name, email, email_verified_at, password, and remember_token:

National ID Cards Table Migration
- In the create_national_id_cards_table migration file, define the fields like id, user_id, full_name, date_of_birth, address, and issued_at. The user_id will be a foreign key linking the national ID card to a user:

Step 3: Create Models for Both Tables
User Model
- Run the following command to generate the User model:
- php artisan make:model User
National ID Card Model
- Run the following command to generate the NationalIdCard model:
- php artisan make:model NationalIdCard
Step 4: Define the One-to-One Relationship in the Models
User Model
- In the User model, use the hasOne method to define that a user has one national ID card:

National ID Card Model
- In the NationalIdCard model, use the belongsTo method to define that a national ID card belongs to a user:

Step 5: Retrieving Data Using One-to-One Relationships
Now that we’ve set up the relationship, you can easily retrieve related data:
Accessing the National ID Card from the User
- To get the national ID card for a user:

Accessing the User from the National ID Card
- To get the user associated with a national ID card:
Eager Loading
Eager loading allows you to load the related model (in this case, the National ID card) in a single query, which improves performance and avoids the N+1 query problem.
- Eager Load the National ID Card with the User

- Eager Load the User with the National ID Card

Conclusion
In Laravel, a One-to-One relationship is a straightforward and powerful way to link related models. By defining relationships in the models and the database, you can easily retrieve, update, and manage related data.
Key points to remember:
- Use the hasOne method in the parent model (User) and belongsTo in the related model (NationalIdCard).
- Store the foreign key (user_id) in the child table (national_id_cards).
- Use eager loading to optimize database queries and reduce the number of database hits.
This approach keeps your data organized, making it easier to manage and scale your application.