One-to-one relationships connect two things where each item matches exactly one item in the other group. It helps keep related data organized and easy to find.
One-to-one (hasOne, belongsTo) in Laravel
class User extends Model { public function profile() { return $this->hasOne(Profile::class); } } class Profile extends Model { public function user() { return $this->belongsTo(User::class); } }
hasOne is used on the model that owns the related model.
belongsTo is used on the model that holds the foreign key.
class User extends Model { public function profile() { return $this->hasOne(Profile::class); } }
class Profile extends Model { public function user() { return $this->belongsTo(User::class); } }
$user = User::find(1);
$profile = $user->profile;$profile = Profile::find(1);
$user = $profile->user;This example shows a User with one Profile. We simulate the relation and print the user's profile bio.
<?php use Illuminate\Database\Eloquent\Model; class User extends Model { public function profile() { return $this->hasOne(Profile::class); } } class Profile extends Model { public function user() { return $this->belongsTo(User::class); } } // Simulate fetching user and profile $user = new User(); $user->id = 1; $profile = new Profile(); $profile->user_id = 1; $profile->bio = 'I love coding!'; // Normally, Laravel fetches from DB, here we simulate // Assume $user->profile() returns $profile // Output user profile bio echo "User ID: {$user->id}\n"; echo "Profile Bio: {$profile->bio}\n";
Make sure the foreign key column (like user_id) exists in the related table.
Use Laravel's naming conventions or specify keys explicitly if different.
One-to-one relations are great for splitting data into smaller tables for clarity.
One-to-one links two models where each has exactly one related record.
Use hasOne on the model owning the other, and belongsTo on the model with the foreign key.
This keeps data organized and easy to access in Laravel.
Practice
Solution
Step 1: Understand ownership in one-to-one
The model that owns the other model is the one that does not have the foreign key.Step 2: Identify the correct method for ownership
In Laravel, the owning model useshasOneto define the relationship.Final Answer:
hasOne -> Option AQuick Check:
Owning model uses hasOne [OK]
- Using belongsTo on the owning model
- Confusing hasOne with hasMany
- Using belongsToMany for one-to-one
Solution
Step 1: Recall belongsTo syntax
The belongsTo method is used on the model with the foreign key and is called likereturn $this->belongsTo(Model::class);.Step 2: Match the correct syntax
public function user() { return $this->belongsTo(User::class); } matches the correct belongsTo syntax exactly.Final Answer:
public function user() { return $this->belongsTo(User::class); } -> Option AQuick Check:
belongsTo syntax uses belongsTo(Model::class) [OK]
- Using hasOne instead of belongsTo
- Forgetting parentheses after belongsTo
- Using belongsToMany for one-to-one
class User extends Model {
public function profile() {
return $this->hasOne(Profile::class);
}
}
class Profile extends Model {
public function user() {
return $this->belongsTo(User::class);
}
}What will
$user->profile->phone return if the user has a profile with phone number '123-456-7890'?Solution
Step 1: Understand the relationship
User has one Profile, so$user->profilereturns the related Profile model instance.Step 2: Access the phone attribute
Accessing$user->profile->phonereturns the phone value from the Profile model, which is '123-456-7890'.Final Answer:
'123-456-7890' -> Option DQuick Check:
hasOne returns single related model [OK]
- Expecting an array instead of a single model
- Confusing belongsTo and hasOne roles
- Assuming null when profile exists
class Author extends Model {
public function biography() {
return $this->hasOne(Biography::class);
}
}
class Biography extends Model {
public function author() {
return $this->belongsTo(Author::class);
}
}But when you try
$author->biography, it returns null even though the biography exists in the database. What is the most likely cause?Solution
Step 1: Check foreign key conventions
Laravel expects the foreign key in Biography table to be 'author_id' by default for belongsTo.Step 2: Identify mismatch causes null
If the foreign key column is named differently, Laravel cannot find the related record, so$author->biographyreturns null.Final Answer:
The Biography model's foreign key column name doesn't match Laravel's default 'author_id'. -> Option CQuick Check:
Foreign key name mismatch causes null relation [OK]
- Switching hasOne and belongsTo methods incorrectly
- Assuming missing method causes null instead of key mismatch
- Ignoring database schema foreign key names
Car and Engine models where each car has exactly one engine. The engines table has a foreign key car_id. Which is the correct way to define the relationship in both models?Solution
Step 1: Identify foreign key location
The foreign keycar_idis in theenginestable, so Engine belongs to Car.Step 2: Define relationships accordingly
Car has one Engine (hasOne), Engine belongs to Car (belongsTo).Final Answer:
Car uses hasOne, Engine uses belongsTo -> Option BQuick Check:
Foreign key in engines means Engine belongsTo Car [OK]
- Reversing hasOne and belongsTo roles
- Using hasMany for one-to-one
- Using belongsToMany for one-to-one
Ready to prove it?
Pass the test and this pattern moves unknown
