Skip to content
Shipu Ahamed edited this page Sep 3, 2016 · 1 revision

source

This is the field or array of fields from which to build the slug. Each $model->field is concatenated (with space separation) to build the sluggable string. This can be model attributes (i.e. fields in the database), relationship attributes, or custom getters.

To reference fields from related models, use dot-notation. For example, the slug for the following book will be generated from its author's name and the book's title:

class Book extends Eloquent
{
    use Sluggable;

    protected $fillable = ['title'];

    public function sluggable() {
        return [
            'slug' => [
                'source' => ['author.name', 'title']
            ]
        ];
    }
    
    public function author() {
        return $this->belongsTo(Author::class);
    }
}
...
class Author extends Eloquent
{
    protected $fillable = ['name'];
}

An example using a custom getter:

class Person extends Eloquent
{
    use Sluggable;

    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'fullname'
            ]
        ];
    }

    public function getFullnameAttribute() {
        return $this->firstname . ' ' . $this->lastname;
    }
}

If source is empty, false or null, then the value of $model->__toString() is used as the source for slug generation.

Clone this wiki locally