ahmadbadpey's avatar

cviebrock/eloquent-sluggable does not work while following all installation guide and reinstalling

I have followed the installation guide in https://github.com/cviebrock/eloquent-sluggable.

This is my Post Model Structure :

namespace App;
use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;
use Illuminate\Database\Eloquent\Model;

class Post extends Model implements SluggableInterface
{
    use SluggableTrait;
    protected $sluggable = [
        'build_from' => 'post_title',
        'save_to'    => 'post_alias',
        'unique'     => true
    ];

    protected $guarded    = ['hits', 'comments_count', 'created_at', 'updated_at'];
    protected $primaryKey = 'post_id';

    protected $dateFormat = 'U';

    public function post_pics ()
    {
        return $this->hasMany('App\PostPics');
    }

    public function categories ()
    {
        return $this->belongsToMany('App\Category', 'category_post', 'post_id', 'cat_id');
    }

    public function getCreatedAtAttribute($value){
        return $value;
    }
}

And added Appropriate entry to service Provider in app.php file :

yajra\Datatables\DatatablesServiceProvider::class,
Spatie\Glide\GlideServiceProvider::class,
Illuminate\Html\HtmlServiceProvider::class,
Cviebrock\EloquentSluggable\SluggableServiceProvider::class,

But when insert a new row to Post Table , post_alias field is null :

$newPost = new Post(['post_title'=>'How are you']);
$newPost->save();

I using Laravel Framework version 5.1.23 (LTS). However when I use this Package in another Project base on However version 5.1.19, it works fine.

Furthermore, I use two Events of this Package to test slugging functionality after insertion:

$newPost = new Post();
                $newPost->post_title    =   'a;lskd als;kdl;as kd;laks ld;kal;sdk';
                $newPost->save();

Post::registerModelEvent('slugging', function($post) {
                    echo 'Hiiiiiiiii';
                });

                Post::registerModelEvent('slugged', function($post) {
                    return $post->getSlug();
                });

But None of them will not run. What is problem and how can I solve that ?

0 likes
3 replies
RomainLanz's avatar

Stupid question but... did you have run migration to add the post_alias field?

ahmadbadpey's avatar

I did not use migrate to create post_alias column and add it manually.

ahmadbadpey's avatar
ahmadbadpey
OP
Best Answer
Level 2

After many search and doing trials and errors, first I remove package then I insert "cviebrock/eloquent-sluggable": "dev-master" exactly after *"laravel/framework": "5.1." **, and before other package in composer.json file.

after updating composer it works fine. I do not know why this occurred.

Please or to participate in this conversation.