Laravel One to One Eloquent Relationship Tutorial

Laravel One to One Eloquent Relationship Tutorial

In this post we will give you information about Laravel One to One Eloquent Relationship Tutorial. Hear we will give you detail about Laravel One to One Eloquent Relationship TutorialAnd how to use it also give you demo for it if it is necessary.

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.

In this example, i will create “users” table and “phones” table. both table are connected with each other. now we will create one to one relationship with each other by using laravel Eloquent Model. We will first create database migration, then model, retrieve records and then how to create records too. So you can also see database table structure on below screen.

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->increments('id');

$table->string('name');

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

$table->string('password');

$table->rememberToken();

$table->timestamps();

});

phones table migration:

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

$table->increments('id');

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

$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 IlluminateNotificationsNotifiable;

use IlluminateFoundationAuthUser as Authenticatable;


class User extends Authenticatable

{

use Notifiable;


/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = [

'name', 'email', 'password',

];


/**

* The attributes that should be hidden for arrays.

*

* @var array

*/

protected $hidden = [

'password', 'remember_token',

];


/**

* Get the phone record associated with the user.

*/

public function phone()

{

return $this->hasOne('AppPhone');

}

}

Phone Model:

<?php


namespace App;


use IlluminateDatabaseEloquentModel;


class Phone extends Model

{

/**

* Get the user that owns the phone.

*/

public function user()

{

return $this->belongsTo('AppUser');

}

}


Retrieve Records:

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

dd($phone);

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

dd($user);

Create Records:

$user = User::find(1);

$phone = new Phone;

$phone->phone = '9429343852';

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

Also see:Laravel One to Many Eloquent Relationship Tutorial

$phone = Phone::find(1);

$user = User::find(10);

$phone->user()->associate($user)->save();

I hope you understand of one to one relationship…

*** Click On it and Read in Details of RelationShip types:

Hope this code and post will helped you for implement Laravel One to One Eloquent Relationship Tutorial. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  67  =  75

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US