Laravel 8 One To One Relationship Example

Websolutionstuff | Nov-01-2021 | Categories : Laravel PHP

In this example we will see laravel 8 one to one relationship example also you can use one to one relationship in laravel 6 and laravel 7. One to one relationship is a very basic type of database relationship. You have one table with primary key that references the id of the other table. For one to one relationship use hasOne and belongsTo method in model for access each other model.

Also we will create migration with foreign key, retrieve records using model, insert new records, update records etc.

We will create User and Phone table. User model might be associated with one Phone model. To define this relationship, we will place a phone method on the User model. The phone method should call the hasOne method and return its result.

Laravel 8 One To One Relationship Example

 

 

Create Migration

Now we have to create migration for users and phones table. we will also add foreign key with users table.

Create Migration of Users Table

Schema::create('users', function (Blueprint $table) {

    $table->increments('id');

    $table->string('name');

    $table->string('email')->unique();

    $table->timestamps();

});

Create Migration of Phones Table with Foreign Key

Schema::create('phones', function (Blueprint $table) {

    $table->increments('id');

    $table->integer('user_id')->unsigned();

    $table->string('phone_no');

    $table->timestamps();
    

    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

});

 

Create Model and Add Relationship on Both Model

In user model we can create phone() function and add relation of phone model using hasOne method.

User Model :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * Get the phone associated with the user.
     */
    public function phone()
    {
        return $this->hasOne(Phone::class);
    }
}

 

 

Phone Model :

we can access the Phone model from our User model. Next, let's define a relationship on the Phone 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.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Phone extends Model
{
    /**
     * Get the user that owns the phone.
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

 if the foreign key on the Phone model is not user_id, you may pass a custom key name as the second argument to the belongsTo method

public function user()
{
    return $this->belongsTo(User::class, 'foreign_key');
}

 

Retrieve Records using Model :

Once the relationship is defined, we may retrieve the related record using Eloquent's dynamic properties. So, here we can use User model with phone function.

$phone = User::find(1)->phone;
$user = Phone::find(1)->user;

 

Create Records using Model 
$user = User::find(1);
 
$phone = new Phone;
$phone->phone_no = '9876543210';
 
$user->phone()->save($phone);

 

 

$phone = Phone::find(1);
 
$user = User::find(1);
 
$phone->user()->associate($user)->save();

 


You might also like :

Recommended Post
Featured Post
Laravel 8 Import Export CSV/EXCEL File Example
Laravel 8 Import Export CSV/EX...

In this article, we will see the laravel 8 import and export CSV/EXCEL file example. We will simple create imp...

Read More

Oct-01-2020

How to Create Custom Login and Registration in Laravel 10
How to Create Custom Login and...

In the ever-evolving landscape of web development, crafting a tailor-made user authentication system stands as a pivotal...

Read More

Aug-25-2023

How To Create Zip File In Laravel 7/8
How To Create Zip File In Lara...

In this tutorial I will give you example of how to create zip file in laravel 7/8. Some times client's have requirme...

Read More

Dec-17-2021

How To Create Dynamic Linechart In Laravel
How To Create Dynamic Linechar...

In this post, we will see how to create a dynamic line chart in laravel. A dynamic line chart or line plot or line...

Read More

Jul-22-2020