One To One Relationship

What is a One-to-One Relationship?

A one-to-one relationship means that a row in one table is linked to exactly one row in another table.

👉 Example:

  • Each employee has exactly one profile.
  • Each profile belongs to exactly one employee.
Employee: 1 ↔ 1 Profile

Flow diagram


Migration:

Use this command to generate a migration for creating a tables:

php artisan make:migration create_one_to_one_tables


Migration File

public function up(): void
{
    Schema::create('employees', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->timestamps();
    });

    Schema::create('profiles', function (Blueprint $table) {
        $table->id();
        $table->foreignId('employee_id')->constrained()->onDelete('cascade');
        $table->string('bio')->nullable();
        $table->timestamps();
    });
}

public function down(): void
{   
    Schema::dropIfExists('profiles');
    Schema::dropIfExists('employees');
}


Run Migration

php artisan migrate

Tables

employees table
id name created_at updated_at
profiles table
id employee_id bio created_at updated_at

Here,

  • profiles.employee_id is a foreign key referencing employees.id.
  • With onDelete('cascade'), if you delete an employee, their profile is also deleted automatically.



Model:

Generate a model with Artisan:

php artisan make:model Employee
php artisan make:model Profile

This generates 2 files Employee.php and Profile.php


Employee.php

namespace App\Models;

use App\Models\Profile;
use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    protected $fillable = ['name'];

    // Relation: An employee has one profile
    public function profile()
    {
        return $this->hasOne(Profile::class);
    }
}


Profile.php

namespace App\Models;

use App\Models\Employee;
use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    protected $fillable = ['employee_id', 'bio'];

    // Relation: A profile belongs to an employee
    public function employee()
    {
        return $this->belongsTo(Employee::class);
    }
}

CRUD Examples for One-to-One

Create:

use App\Models\Employee;

// Method 1
// Create employee
$employee = Employee::create(['name' => 'Watson']);

// Create profile and attach to employee
$employee->profile()->create([
    'bio' => 'Senior Laravel Developer',
]);


// Method 2 Using Eloquent instance
$employee = new Employee();
$employee->name= "John";
$employee->save();

// Create profile and attach to employee
$employee->profile()->create([
    'bio' => 'PHP Developer',
]);
Output:
employees table
id name created_at updated_at
1 Watson 2025-08-17 20:38:58 2025-08-17 20:38:58
2 John 2025-08-17 20:38:58 2025-08-17 20:38:58
profiles table
id employee_id bio created_at updated_at
1 1 Senior Laravel Developer 2025-08-17 20:38:58 2025-08-17 20:38:58
2 2 PHP Developer 2025-08-17 20:38:58 2025-08-17 20:38:58

Read

use App\Models\Employee;
use App\Models\Profile;

// Get employee with profile
$employee = Employee::find(1);
echo $employee->profile->bio; // Senior Laravel Developer

// Get profile with employee
$profile = Profile::find(1);
echo $profile->employee->name; // John


Update

use App\Models\Employee;

// Update profile bio of employee
$employee = Employee::find(1);
$employee->profile->update(['bio' => 'Updated Bio']);

Delete

use App\Models\Employee;
use App\Models\Profile;

// Delete employee -> Profile auto deleted (cascade)
Employee::find(1)->delete();

// Or delete profile only not delete employee
Profile::find(2)->delete();

Eager loading (optimizes queries)

use App\Models\Employee;
use App\Models\Profile;

// Get employee with profile
$employee = Employee::with('profile')->find(1);
echo $employee->profile->bio; // Senior Laravel Developer

// Get profile with employee
$profile = Profile::with('employee')->find(1);
echo $profile->employee->name; // John

 Always use with() when fetching related data in bulk.


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.