Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

In this tutorial, i would like to explain one to one model relationship in laravel. One to One model relationship is very simple and basic. you have to make sure that one of the table has a key that references the id of the other table. we will learn how we can create migration with foreign key schema, retrieve records, insert new records, update records etc.

One to One Relationship will use "hasOne()" and "belongsTo()" for relation.

Create Migrations:

Now we have to create migration of "users" and "phones" table. we will also add foreign key with users table. so let's create like as below:

 

users table migration:


        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

phones table migration:

        Schema::create('phones', function (Blueprint $table) {
            $table->increments('id');
            $table->foreignId('user_id');
            $table->string('phone');
            $table->timestamps();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });

 

Create Models:

Here, we will create User and Phone table model. we will also use "hasOne()" and "belongsTo()" for relationship of both model.

 

User Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

 

Phone Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

 

Retrieve Records:

Retrieve phone from User object:

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

Retrieve user from Phone object:

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

 

Create Records:

Eloquent provides convenient methods for adding new models to relationships. For example, you need to insert a new Phone for a User model. Instead of manually setting the user_id attribute on the Phone, you may insert the Phone directly from the relationship's save method:

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

When updating a belongsTo relationship, you may use the associate method. This method will set the foreign key on the child model:

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

 

I hope you found this article useful. let me know if you have any questions and I’ll be happy to answer them.

Comments

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon