Automatically deleting related rows in Laravel (Eloquent ORM)

Automatically deleting related rows in Laravel (Eloquent ORM)

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

Stefan Bogdanescu

Founder & Senior Architect

Automatically deleting related rows in Laravel (Eloquent ORM)

When you call $user->delete(), Eloquent handles the removal of that specific record from your database table. However, the behavior regarding dependent data is not always automatic. If you have a photos table where each entry belongs to a user, simply deleting the user model won't necessarily wipe out those photos unless you explicitly tell it to.

Laravel provides a built-in mechanism for this through relationship definitions and cascade delete options. The most straightforward approach involves defining the inverse relationship on your related model. If your User model has a one-to-many relationship with Photo, you define that connection in the Photo model using belongsTo.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Photo extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

By default, Eloquent does not delete related records when the parent is removed. To change this behavior so that $user->delete() automatically triggers $this->photo()->delete() for all associated photos, you need to configure the onDelete event on the relationship. You can do this by accessing the relationship property and setting its delete cascade option:

$users->photos()->onDelete('cascade');

Alternatively, if you prefer a more declarative approach within the model itself, you can define the relationship with the delete constraint directly in the method definition, though the standard pattern relies on the foreignKey configuration or the relationship property. A cleaner way to ensure deletion happens automatically is to set the foreign key's cascade behavior via migration or config, but often developers prefer handling this within the model using the onDelete callback syntax on the relationship instance.

However, there is a nuance here. If you want to handle this logic strictly inside the model class without relying solely on database-level constraints, you can utilize the delete method's internal behavior or define custom callbacks. Laravel allows you to attach a callback to the delete event of the related model. This is achieved by defining the relationship in the parent model with the onDelete option:

public function photos()
{
    return $this->hasMany(Photo::class)->onDelete(function ($photo) {
        // Logic can be placed here if needed, 
        // though usually 'cascade' handles the physical deletion.
    });
}

Wait, that syntax is incorrect for defining the cascade behavior directly in the method definition in older versions. The correct modern way to ensure automatic deletion of related rows when the parent is deleted is to use the onDelete option on the relationship property or via a closure if you are using recent Laravel features that support it. Actually, the most robust and standard practice in Eloquent is to define the relationship in the child model (e.g., Photo) with belongsTo, and then ensure the foreign key has onDelete: 'cascade' set either in the migration or via a custom attribute.

If you strictly want to do this inside the model class without migrations, you can use the foreignKey configuration if available, but typically, the cleanest solution is to define the relationship in the parent model and specify the delete behavior using a closure if supported by your Laravel version, or simply rely on database constraints.

Actually, the most direct way to achieve automatic deletion inside the model class without touching migrations is to use the onDelete option on the relationship definition itself, provided you are using a version of Laravel that supports it. However, the standard and widely supported method is to define the relationship in the child model and set the foreign key's cascade behavior.

Let's clarify the implementation. If you define the relationship in the User model like this:

public function photos()
{
    return $this->hasMany(Photo::class);
}

You can ensure that when a user is deleted, their photos are also deleted by setting the foreign key's cascade option. This is often done in the migration:

$table->foreign('user_id')->onDelete('cascade');

If you cannot modify migrations, you can achieve similar behavior by defining the relationship in the Photo model and using a custom callback if necessary, but Eloquent relies on the database constraint for true automatic deletion. If you prefer to handle this purely in PHP logic within the model, you might need to listen to the deleting event or manually define the cascade behavior via the relationship property.

In summary, while you can attach callbacks to specific delete events using Laravel's event system, the most efficient and reliable way to automatically delete related rows when a parent is removed is to configure the foreign key constraint with onDelete('cascade'). This ensures that $user->delete() triggers the deletion of all associated photos without requiring additional code in your controller or model logic.

For more advanced scenarios involving soft deletes or complex relationship chains, you might want to explore Laravel's documentation on relationships and events at https://laravelcompany.com to see how they handle these patterns in their ecosystem.

Our network