Laravel Eloquent One to One Relationship Tutorial with Example

A one-to-one relationship is a very basic relation. In this guide, I’m going to explain one to one eloquent relationship model. I’ll show how to insert data and retrieve data using the eloquent model.

In this tutorial, I’ll create two tables called users and mobile. Here’s the schema design:

Note: This article last tested on Laravel 7.x. But it’ll work on Laravel 8.x too. Just define models in app/Models folder. That’s it.

Table of Contents

  1. Install Laravel and Basic Configurations
  2. Create Migration and Model
  3. Setup One To One Relationship
  4. Inverse Of The Relationship
  5. Insert Records
  6. Retrieve Records
  7. Update Records
  8. Delete Records

Step 1 : Install Laravel and Basic Configurations

Each Laravel project needs this thing. That’s why I have written an article on this topic. Please see this part from here: Install Laravel and Basic Configurations.

Step 2 : Create Migration and Model

We have already the User model and migration file. Let’s create a model named Mobile with a migration file. Run this command to create both:

php artisan make:model Mobile -m

Now open the migration file for the Mobile model from database>migrations directory. Then paste the below code in the up() function of the migration file.

public function up()
    Schema::create('mobiles', function (Blueprint $table) {


We have set user_id as a foreign key and mobile number will be deleted if we delete the user.

Step 3 : Setup One To One Relationship

We have two models User & Mobile. For example, a User model might be associated with one Mobile. To define this relationship, we place a mobile method on the User model. The mobile method should call the hasOne method and return its result.


namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
    // ...

     * Get the mobile number associated with the user.
    public function mobile()
        return $this->hasOne(Mobile::class);
        // note: we can also inlcude Mobile model like: 'App\Mobile'

Step 4 : Inverse Of The Relationship

So, we can access the Mobile model from our User. Now, let’s define a relationship on the Mobile model that will let us access the User that owns the phone. We can define the inverse of a hasOne relationship using the belongsTo method.

Open Mobile model and paste this code:


namespace App;

use Illuminate\Database\Eloquent\Model;

class Mobile extends Model
     * Get the user that owns the mobile.
    public function user()
        return $this->belongsTo(User::class);

Step 5 : Insert Records

Let’s add data to user and mobile from controller:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Mobile;
use Hash;

class UserController extends Controller
    public function addUserMobile()
        $user = new User;
        $user->name = "Test Name";
        $user->email = "[email protected]";
        $user->password = Hash::make("12345678");

        $mobile = new Mobile;
        $mobile->mobile = '123456789';

Step 6 : Retrieve Records

We can easily get data from relation model like this:

public function index()
    // get user and mobile data from User model
    $user = User::find(1);

    // get user data from Mobile model
    $user = Mobile::find(1)->user;

    // get mobile number from User model
    $mobile = User::find(1)->mobile;

Step 7: Update Records

Let’s update the user’s table data and mobile’s table data at once:

public function update()
    $user = User::find(1);

    $user->name = 'Test II';
    $user->mobile->mobile = '987654321';

Step 8 : Delete Records

We can easily delete both table’s data at once. Here’s the example:

public function delete()
    $user = User::find(1);

To get more details of one to one eloquent relationship, you can read Laravel’s official documentation.

The tutorial is over. Thanks for reading.