Laravel One to One Eloquent Relationship Tutorial
In this tutorial, i would like to explain one to one model relationship in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 app. 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->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->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\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User 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('App\Phone');
}
}
Phone Model:
<?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('App\User');
}
}
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);
$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:
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Eloquent whereNotBetween() Query Example
- Laravel Eloquent whereBetween() Query Example
- Laravel Eloquent selectRaw() Query Example
- Laravel Order By Relation Column Example
- Laravel Order By Relationship Sum Column Example
- Laravel Relationship Eager Loading with Condition Example
- Laravel Relationship Eager Loading with Count Example
- Laravel Relationship Where Condition Example
- Laravel Eloquent Relationships Tutorial From Scratch
- Laravel One to Many Eloquent Relationship Tutorial
- Laravel Many to Many Eloquent Relationship Tutorial
- Laravel Has Many Through Eloquent Relationship Tutorial
- Laravel One to Many Polymorphic Relationship Tutorial
- Laravel Many to Many Polymorphic Relationship Tutorial