Laravel generate slug before save

Laravel generate slug before save

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

Stefan Bogdanescu

Founder & Senior Architect

Laravel generate slug before save

You are encountering a fundamental quirk of how Eloquent handles mass assignment and attribute mutators. When you define protected $fillable = ['title', 'text', 'subtitle'], you are explicitly restricting which attributes can be filled via mass assignment operators like $activity->fill() or creating a model instance from a request array.

The core issue lies in the interaction between your setSlugAttribute method and the $fillable property. Because 'slug' is not listed in $fillable, Eloquent ignores any attempt to set it directly on the model instance during mass assignment, even if you try to assign it inside a mutator triggered by another field. However, there is a more critical detail regarding how the title attribute itself behaves.

In your second attempt, you defined setTitleAttribute but did not add 'title' to the $fillable array. If you are creating an Activity model using mass assignment (e.g., Activity::create(['title' => 'My New Title'])), Eloquent will reject setting the title field entirely because it is missing from the whitelist. This explains why your title field appeared empty; the setter was never triggered because the assignment failed before reaching the logic inside your mutator.

To fix this, you must ensure that every attribute you intend to write to—both the source (title) and the destination (slug)—is included in the $fillable array. Once 'title' and 'slug' are both whitelisted, Eloquent will allow the setTitleAttribute method to run when you save a new record.

Here is the corrected implementation that ensures the slug is generated automatically based on the title:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Activity extends Model {
    protected $table = 'activitys';

    // Ensure both source and destination are fillable
    protected $fillable = [
        'title',
        'text',
        'subtitle',
        'slug'
    ];

    // Mutator for title to generate slug automatically
    public function setTitleAttribute($value)
    {
        $this->attributes['title'] = $value;
        
        // Generate slug from the current title value
        $this->attributes['slug'] = str_slug($value, '-');
    }

    // Optional: Mutator for slug if you ever want to set it manually without affecting title
    public function setSlugAttribute($value)
    {
        $this->attributes['slug'] = $value;
    }
}

The parameter passed to setSomeAttribute, such as $value in the examples above, represents the raw data being assigned to that specific column. When you use mass assignment, Eloquent calls these mutators with the incoming value. Inside the mutator, you can perform transformations before storing the final value in the model's internal attributes array.

By placing the generation logic inside setTitleAttribute, you ensure that whenever a new title is provided, the corresponding slug is calculated immediately. This approach keeps your database consistent without requiring complex observers or event listeners. It also makes testing straightforward; you simply instantiate the model with a title and save it, trusting the mutator to handle the rest.

One trade-off to consider is that this logic only applies during mass assignment or direct property setting via setters. If you populate the $attributes array directly (bypassing fillable rules) or use the forceFill method, the mutators will not be triggered. This is why it is crucial to keep your fillable whitelist aligned with your business logic.

If you prefer a cleaner syntax where the slug is derived automatically without needing to explicitly list it in $fillable (though this restricts direct mass assignment of the slug), you could use an observer or a relationship, but for a simple one-to-one transformation, the mutator pattern shown above remains the most performant and readable solution.

For further reading on Eloquent mutators and casting, resources like https://laravelcompany.com provide excellent documentation on managing model attributes efficiently.

Our network