Laravel

Laravel One to One Eloquent Relationship Tutorial

In Laravel, Eloquent is an ORM (Object-Relational Mapping) that simplifies database operations and provides a convenient way to define and work with relationships between database tables. In this tutorial, we will explore the one-to-one Eloquent relationship and learn how to set it up in Laravel.

Step 1: Database Setup
Before we start, make sure you have a Laravel project set up and configured with a database connection. Create two tables: `users` and `profiles`.

users table:
- id (primary key)
- name
- email

profiles table:
- id (primary key)
- user_id (foreign key referencing users.id)
- bio

Step 2: Define Eloquent Models
In Laravel, models represent database tables. Create two models: `User` and `Profile`.

// User.php


namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
   public function profile()
   {
       return $this->hasOne(Profile::class);
   }
}

// Profile.php
 

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
   public function user()
   {
       return $this->belongsTo(User::class);
   }
}

Explanation:
- The `User` model defines a `hasOne` relationship with the `Profile` model. It means that each user has one profile.
- The `Profile` model defines a `belongsTo` relationship with the `User` model. It establishes the reverse relationship, stating that each profile belongs to a user.

Step 3: Retrieving Records
Now, let's see how to retrieve user and profile records using the one-to-one relationship.

// Retrieve a user's profile
$user = User::find(1);
$profile = $user->profile;

// Retrieve a profile's user
$profile = Profile::find(1);
$user = $profile->user;

Explanation:
- We can retrieve a user's profile by accessing the `profile` relationship on the `User` model.
- Similarly, we can retrieve a profile's user by accessing the `user` relationship on the `Profile` model.

Step 4: Creating Records
Let's see how to create user and profile records and establish the one-to-one relationship between them.

// Create a user with a profile

$user = new User;
$user->name = "John Doe";
$user->email = "john@example.com";
$user->save();

$profile = new Profile;
$profile->bio = "I am a Laravel developer";
$user->profile()->save($profile);

Explanation:
- We create a new `User` instance and set its attributes.
- We save the user to the database.
- We create a new `Profile` instance and set its attributes.
- We save the profile using the `profile` relationship on the `User` model.

Step 5: Updating Records
To update user and profile records, we can use the same approach as creating records.

// Update a user's profile
$user = User::find(1);
$user->name = "Updated Name";
$user->profile->bio = "Updated bio";
$user->save();
$user->profile->save();

Explanation:
- We retrieve the user and update its attributes.
- We access the associated profile through the `profile` relationship and update its attributes.
- We save both the user and the profile to persist the changes.

Leave A Comment