0
0
Laravelframework~10 mins

One-to-one (hasOne, belongsTo) in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - One-to-one (hasOne, belongsTo)
Define Model A
Add hasOne relation in Model A
Define Model B
Add belongsTo relation in Model B
Query Model A -> get related Model B
Query Model B -> get related Model A
This flow shows how to set up and use one-to-one relations between two models using hasOne and belongsTo in Laravel.
Execution Sample
Laravel
class User extends Model {
  public function phone() {
    return $this->hasOne(Phone::class);
  }
}

class Phone extends Model {
  public function user() {
    return $this->belongsTo(User::class);
  }
}
Defines a one-to-one relation where a User has one Phone, and a Phone belongs to a User.
Execution Table
StepActionModelRelation Method CalledQuery GeneratedResult
1Call user->phone()UserhasOneSELECT * FROM phones WHERE user_id = ?Phone model instance or null
2Call phone->user()PhonebelongsToSELECT * FROM users WHERE id = ?User model instance or null
3Access phone property on userUserproperty accessUses cached relationPhone instance returned
4Access user property on phonePhoneproperty accessUses cached relationUser instance returned
5No related record foundUser or Phonerelation callQuery returns emptynull returned
💡 Queries stop after fetching related model or returning null if none found
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
userUser instanceUser instanceUser instanceUser instance with phone loadedUser instance with phone loaded
phonenullPhone instance or nullPhone instance or nullPhone instancePhone instance
phone->usernullnullUser instance or nullUser instanceUser instance
Key Moments - 3 Insights
Why does user->phone() return null sometimes?
Because the related phone record may not exist in the phones table for that user_id, as shown in execution_table step 5.
Why do we use hasOne in User and belongsTo in Phone?
hasOne defines the owning side with foreign key in the other table; belongsTo defines the inverse side with foreign key in this model, as shown in execution_sample.
Does accessing user->phone cache the result?
Yes, after the first query, Laravel caches the related model, so repeated access does not run new queries, as shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what query runs when calling phone->user()?
ASELECT * FROM phones WHERE user_id = ?
BSELECT * FROM users WHERE id = ?
CSELECT * FROM phones
DSELECT * FROM users
💡 Hint
Check execution_table row 2 under 'Query Generated'
At which step does Laravel return null if no related record exists?
AStep 5
BStep 1
CStep 3
DStep 4
💡 Hint
Look at execution_table row 5 'Result' column
If you remove belongsTo from Phone, what happens when calling phone->user()?
AIt returns the User instance anyway
BIt runs the hasOne query instead
CIt throws an error or returns null
DIt caches the phone relation
💡 Hint
belongsTo defines the inverse relation needed for phone->user(), see execution_sample
Concept Snapshot
One-to-one relations link two models where one owns the other.
Use hasOne in the owning model to define the relation.
Use belongsTo in the owned model to define the inverse.
Queries fetch related model by foreign key.
Accessing relation caches the result for reuse.
Full Transcript
In Laravel, one-to-one relations connect two models so each instance of one model relates to exactly one instance of another. We define hasOne in the model that owns the other, meaning it expects the foreign key in the related model's table. The related model uses belongsTo to point back. When you call the relation method, Laravel runs a query to find the related record by foreign key. If found, it returns the related model instance; if not, it returns null. Accessing the relation caches the result to avoid repeated queries. This setup helps keep data connected simply and clearly.