'integer', ]; protected static function boot() { parent::boot(); static::creating(function (ArticleCategory $category) { if (empty($category->slug)) { $slug = Str::slug($category->name); if (empty($slug)) { $slug = 'category-'.time(); } $originalSlug = $slug; $count = 1; while (static::where('slug', $slug)->exists()) { $slug = $originalSlug.'-'.$count++; } $category->slug = $slug; } }); } public function articles() { return $this->belongsToMany(Article::class, 'article_category', 'category_id', 'article_id'); } }