cviebrock / eloquent-taggable

Easily add the ability to tag your Eloquent models in Laravel.
MIT License
537 stars 72 forks source link

Use Taggable With cviebrock/sluggable on Normalized Column? #94

Closed reasecret closed 5 years ago

reasecret commented 5 years ago

How can I use sluggable to insert tag slugs to normalized column?

At config/taggable.php I've changed normalizer like this but not worked: 'normalizer' => ['Cviebrock\EloquentSluggable\Sluggable', 'slug'],

cviebrock commented 5 years ago

You want to slug your model with the normalized name of the model's tag?

If so (assuming things run in the right order, which I'm not sure they do), you should probably create an accessor on your model something like:

public function getSlugSourceNameAttribute() {
    return $this->tags->first()->normalized;
}

And then in your sluggable configuration, set the source of the slug to be "slug_source_name".

reasecret commented 5 years ago

I just want to save tag's slugged version in to normalized column with your sluggable package. For example: "apple tag" to "apple-tag".

Sorry for misunderstood.

cviebrock commented 5 years ago

Ah, I see.

Well, you could make things really simple by not including Sluggable and by just doing this in your taggable.php configuration:

'normalizer' => [\Illuminate\Support\Str::class, 'slug'],
cviebrock commented 5 years ago

If you really want to use the sluggable package (thanks!), you'd have to jump through a few hoops:

'normalizer' => function($string) {
    return \Cviebrock\EloquentSluggable\Services\SlugService::createSlug(
        \Cviebrock\EloquentTaggable\Models\Tag::class,
        'normalized',
        $string
    );
},

The above is untested, but something like that should work. However, you then can't cache your Laravel config. Plus, the overhead is pretty high ... when you probably get good-enough behaviour using Str::slug as mentioned above.

reasecret commented 5 years ago

You're right. I want to use Sluggable but I can't run config:cache command like you said. There was an error. I'm using Str::slug now. Thanks for your replies.