Saving one to one relation in Laravel

Saving one to one relation in Laravel

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Saving One-to-One Relations in Laravel Without Duplication

Encountering duplicate records when updating a one-to-one relationship is a common frustration, but it almost always stems from how Eloquent determines whether an entity needs to be persisted. The core issue isn't a glitch in Laravel's relationship logic; it is a mismatch between the object state you are passing and the foreign key constraints defined on your database table.

When you define a Company as belonging to a User, you establish a foreign key constraint where the companies table contains a user_id column referencing the users table. In your model, this is correctly represented by:

public function user() {
    return $this->belongsTo(User::class, 'user_id');
}

Conversely, the User model defines the inverse relation using hasOne. This tells Eloquent that a single user can have exactly one company. The definition looks like this:

public function company() {
    return $this->hasOne(Company::class, 'user_id');
}

The critical detail you must understand is that hasOne implicitly assumes the foreign key resides on the child model (Company). When you access $user->company, Eloquent expects to find a record where companies.user_id matches the current user's ID.

The Root Cause: Missing Foreign Key Assignment

The problem in your controller code arises from how you are instantiating and saving the Company object:

$company = new Company();
$company->name = 'Test';
User::findOrFail(1)->company()->save($company);

Here, $company is a fresh instance with no attributes set. When you call save() on the relationship accessor ($user->company), Eloquent attempts to hydrate this new model. However, the new Company instance does not have its user_id populated.

If your database schema strictly enforces referential integrity (which is standard practice), inserting a row into companies without a valid user_id will fail immediately. However, if you are bypassing strict checks or using nullable keys, Eloquent might default the ID to NULL.

But why does it create a new entry instead of updating? It creates a new entry because the object being saved is completely unattached to an existing record in the database. Laravel treats $company as a brand-new entity. Even though you are calling save() on the relationship accessor, that method simply delegates to $company->save(). Since $company lacks a primary key (it's a fresh instance) and lacks the foreign key association required to locate an existing row, Eloquent executes an INSERT query rather than an UPDATE.

The Correct Approach: Hydrate with Foreign Keys

To update an existing one-to-one relationship, you must explicitly link the new model instance to the parent record before saving it. You need to assign the foreign key (user_id) to the $company instance.

Here is the corrected implementation:

$user = User::findOrFail(1);
$existingCompany = $user->company; // Loads existing company if present, null otherwise

$newCompanyData = [
    'name' => 'Test',
];

// If a company already exists, we update it.
if ($existingCompany) {
    $existingCompany->update($newCompanyData);
} else {
    // Create new company with the foreign key set immediately
    $newCompany = new Company($newCompanyData);
    $newCompany->user_id = $user->id; 
    $newCompany->save();
}

Alternatively, if you prefer the fluent interface style often used for creating associations:

$user = User::findOrFail(1);

$company = new Company();
$company->name = 'Test';
$company->user_id = $user->id; // Crucial: Set the foreign key here

if (!$user->company) {
    $user->companies()->create($company);
} else {
    $user->company->update(['name' => 'Test']);
}

Handling the "Orphan" Scenario

If you attempt to overwrite an existing Company record without ensuring the foreign key is set, Eloquent will not know which row in the companies table corresponds to your $company object. Without a primary key or a valid foreign key to anchor the update query, Eloquent defaults to inserting a fresh row.

To prevent duplication, always ensure that:

  1. Foreign Keys are Populated: Before saving an orphaned model instance in a one-to-one relation, manually set the user_id (or whatever the foreign key column is named).
  2. Use Update Methods When Appropriate: If you have access to the existing relationship via $user->company, calling update() directly on that relationship is often safer and cleaner than passing a detached model instance to save().

By explicitly managing the foreign key state, you ensure Eloquent correctly identifies whether it needs to perform an INSERT or an UPDATE. This pattern applies universally to one-to-one relationships in Laravel. If you need deeper insights into relationship management patterns for your specific architecture, resources from https://laravelcompany.com can provide additional context on advanced association strategies.

Our network