Backend Development 6 min read

Defining and Using Laravel Eloquent Relationships (One-to-One, Inverse, and Default Models)

This article explains how to define and use Laravel Eloquent relationships—including one-to-one, inverse, and default model handling—by showing the necessary model methods, query constraints, and code examples for both sides of the association.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Defining and Using Laravel Eloquent Relationships (One-to-One, Inverse, and Default Models)

Database tables are often related (e.g., a blog post has many comments, an order belongs to a user). Laravel's Eloquent ORM makes managing these relationships simple and supports various types such as one-to-one, one-to-many, many-to-many, polymorphic, and more.

Relationships are defined as methods on Eloquent model classes. Like the models themselves, relationships can be used as powerful query builders, allowing chainable constraints. For example, you can add a condition to the posts relationship:

$user->posts()->where('active', 1)->get();

One-to-One Relationship

A one-to-one relationship links a single record in one table to a single record in another. For instance, a User model may have one associated Phone model. Define the relationship in the User model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * Get the phone record associated with the user.
     */
    public function phone()
    {
        return $this->hasOne('App\Phone');
    }
}

After defining the method, you can access the related phone via a dynamic property:

$phone = User::find(1)->phone;

Eloquent determines the foreign key name based on the model name, assuming Phone has a user_id column. To override the default foreign key, pass a second argument to hasOne :

return $this->hasOne('App\Phone', 'foreign_key');

To also customize the local key, provide a third argument:

return $this->hasOne('App\Phone', 'foreign_key', 'local_key');

Defining the Inverse Relationship

Now define the inverse on the Phone model so you can retrieve the owning User :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Phone extends Model
{
    /**
     * Get the user that owns this phone.
     */
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

If the foreign key on Phone is not user_id , specify it as the second argument:

return $this->belongsTo('App\User', 'foreign_key');

When the parent model uses a custom primary key or you need a different column for the relationship, provide a third argument:

return $this->belongsTo('App\User', 'foreign_key', 'other_key');

Default Models with withDefault

The belongsTo relationship can define a default model that is returned when the related record does not exist, implementing the Null Object pattern:

public function user()
{
    return $this->belongsTo('App\User')->withDefault();
}

You can also supply an attribute array to populate the default model:

return $this->belongsTo('App\User')->withDefault([
    'name' => '游客',
]);

Or use a closure for more complex default initialization:

return $this->belongsTo('App\User')->withDefault(function ($user) {
    $user->name = '游客';
});

These techniques allow you to work with relationships safely without needing additional null checks.

backendDatabaseORMphpLaravelEloquentRelationships
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.