Video Course

Laravel Eloquent Relationships: Lesson 2 of 7

One to One

Up Next

One to Many

Autoplaying in 7 seconds!

Cancel

Create a Free Account to Watch

Sign Up Now

Login to your account

One to one relationships are very straightforward in Laravel. Make sure that one of the tables has a key that references the id of the other table.

For example, an addresses table will have a field called user_id. That will reference the users.id field. Eloquent will handle the rest.

// app/User.php

...

public function address() {
    return  his->hasOne('App\Address');
}

...

To save a one-to-one relationship:

$user->address()->save($address);

Chris Sevilleja

156 posts

Co-founder of Scotch.io. Slapping the keyboard until something good happens.