One To One Relationship Laravel 9 Example

In this article, we will see one to one relationship in laravel 9 example. A one-to-one relationship is a very basic type of database relationship. You have one table with a primary key that references the id of the other table. 

For one to one relationships use hasOne and belongsTo the method in the model for access to each other model. Also, you can use one to one relationship in laravel 6 and laravel 7, and laravel 8.

So, let's see the laravel 9 one to one relationship, laravel 9 eloquent relationships example.

We will create a 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.

one_to_one_relationship_laravel_9

 

Create Migration

In this step, we have to create migration for users and phones table. we will also add a foreign key to the user's 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 the user model, we can create a phone() function and add the relation of the 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, you may retrieve the related record using Eloquent's dynamic properties. So, here we can use the User model with a phone function.

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

 

 

Create Records using Model 

Now, we will add records to the phone table using the user model.

$user = User::find(1);
 
$phone = new Phone;
$phone->phone_no = '1234567890';
 
$user->phone()->save($phone);

 

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

 


You might also like:

RECOMMENDED POSTS

FEATURE POSTS