Laravel one to one relationship

Laravel one to one relationship

In this article let’s understand one-to-one relationship in Laravel.

Laravel provides many relationship to work with multiple tables and the very basic one is one-to-one relationship.

So, when one record of the table is associated with only one record of another table we can define one to one relationship.

Check below graphics to understand it in simple scenario.

What you can see from graphics is one student is associated with one profile and similarly one profile will be having only student.

Let’s understand it through example where we have:

  • Two models : Student and Profile
  • Two migrations: For Student and Profile table

Create them using below artisan command :

php artisan make:model Student -m
php artisan make:model Profile -m

This will create two models and two migrations as below:

app/Models/
    Student.php
    Profile.php

database/migrations/
    ***_create_profiles_table.php
    ***_create_students_table.php

Open ***_create_profiles_table.php and modify as blow:

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

Check ***_create_students_table.php and modify as blow:

public function up(): void
    {
        Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->string('first_name');
            $table->string('last_name');
            $table->unsignedBigInteger('profile_id')
            $table->timestamps();
        });
    }

Open app/Models/Student.php and modify as blow:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    use HasFactory;
    public function profile()
    {
        return $this->hasOne(Profile::class);
    }
}

Check app/Models/Profile.php and modify as blow:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    use HasFactory;
    public function student()
    {
        return $this->belongsTo(Student::class);
    }
}

A relationship between a User model and a Profile model will be the hasOne method, while a relationship between a Profile model and a User model will be the belongsTo method.

Run Migrations:

php artisan migrate

Remember that the foreign key will be on the table of the model that “belongs to” the other in a one-to-one relationship. The profile_id column in the users database in this example links to the id column in the profiles table.

Use below methods in your controller to check how one-to-one relationship works:

// Create a new student and associate a profile with it
$student = new Student([
    'first_name' => 'Jack',
    'last_name' => 'Roy'
]);
$student->save();

$profile = new Profile([
    'standard' => '2',
    'performance' => 'good',
]);
$student->profile()->save($profile);

// Access the student's profile
$profile = $student->profile;
echo $profile->performance; // Output: good

// Access the student of a specific profile
$profile = Profile::find(1);
$student = $profile->student;
echo $student->first_name; // Output: Jack

That’s it on Laravel one to one relationship. Hope this finds you helpful.

See other articles on Laravel here.