Search code examples
phplaravelpackageslug

Laravel cviebrock/eloquent-sluggable how to i generate slug with name and record id


I have used this "cviebrock/eloquent-sluggable" laravel package and i need to generate product slug like this

Name: Red Shirt

Slug: red-shirt-520 (520 is record id for red shirt product)

Following is model function for generate slug but not working correctly

public function sluggable()
{
    return [
        'slug' => [
            'source' => ['name', 'tbl_products.id'],
            'separator' => '-',
            'onUpdate' => true,
        ]
    ];
}

Solution

  • In model file add code as shown below and add the plugin details in config/app.php providers array Cviebrock\EloquentSluggable\ServiceProvider::class,

    use Illuminate\Database\Eloquent\Model;
    use Cviebrock\EloquentSluggable\Sluggable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    
    class User extends Authenticatable
        {
         use Sluggable;
         public function sluggable() {
            return ['slug'=>[
                'source'=>'full_name',
                'onUpdate'=>true
            ]];
         }
        }