cviebrock / eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel
MIT License
3.9k stars 460 forks source link

BelongsToMany relation #575

Closed neway02 closed 2 years ago

neway02 commented 2 years ago

Hi. Thank you very much for this package. Is it possible to make a slug in a pivot table? I have a Categories table and a Languages table. I have a pivot table category_language where I store category translations. It contains id, category_id, language_id, title, slug attributes. I'd like to automatically title the slug in this pivot table when I save Category model. Any ideas for this?

neway02 commented 2 years ago

1

CategoryController

    $category = Category::create();
    $category->translations()->attach(1, ['title' => 'Тест 1']);

CategoryModel

public function translations()
{
    return $this->belongsToMany(Language::class, 'category_language', 'category_id', 'language_id')
        ->as('translate')
        ->withPivot('title', 'slug');
}
cviebrock commented 2 years ago

I've not tried this myself, but I suspect you'd need to use a custom pivot model and that pivot model would need to implement the Sluggable package.

neway02 commented 2 years ago

Thanks for the answer. I was hoping that this way of working has already been used by someone. But it's not a problem. Thanks again

neway02 commented 2 years ago

If anyone needs to find a solution to use the module along with a pivot table.