In this tutorial of explained of laravel one to one relationship example. One to One relationship is very simple and basic. You have make sure that one of the table has a key that references the id of the other table. This type of relationship is used when single record in one table is associated with a single record in another table.
We will explore how to integrate and use laravel one to one relationship using eloquent. In this example.
To define a laravel one to one relationship, you need to create a relationship method in your model that uses the ‘hasOne’ or ‘belongTo’ methods. you consider following methods.
Defining a laravel one to one relationship example
Example: User and Detail Relationship
Suppose you have a ‘User’ model and a ‘Detail’ model, and each user has one profile.
Read also: How to get last executed query in Laravel?
Step 1: Database migration
First, create the database tables. The ‘Details’ table should have a foreign key that references the ‘users’ table.
Users Migration is default that will already exists in your project. after that you need create details table migration. you can consider following command.
php artisan make:migaration details
Those command create a migartion file. Like below example.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('details', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->string('information');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('details');
}
};
Step 2: Defining the relationship in models
User Model
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
public function detail()
{
return $this->hasOne(Detail::class);
}
}
Detail Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Detail extends Model
{
use HasFactory;
public function user()
{
return $this->belongsTo(User::class);
}
}
Step 3: Using the relationship
Now that the relationship is defined, you can easily retrieve the associated records.
Retrieving the Detail of a User
$user = User::find(1);
$detail = $user->detail;
Retrieving the User of a Detail
$detail = Detail::find(1);
$detail = $detail->user;
Step 4: Creating and saving related models
You can also create and save related models easily. you can consider following method.
$user = User::find(1);
$detail = new Detail(['information' => 'This is my information']);
$user->detail()->save($detail);
Read also: How to get last executed query in Laravel?
I hope this tutorial help you.