public static function boot() { parent::boot(); Category::creating(function ($category) { $category->full_permalink = Category::generateFullPermalink($category, $category->permalink); $category->order = Category::where('blog_category_id', '=', $category->blog_category_id)->count() + 1; }); Category::updating(function ($category) { $category->full_permalink = Category::generateFullPermalink($category, $category->permalink); if (count($category->categories)) { foreach ($category->categories as $category) { $category->full_permalink = Category::generateFullPermalink($category, $category->permalink); $category->save(); } } if (count($category->articles)) { foreach ($category->articles as $article) { $article->full_permalink = Article::generateFullPermalink($article, $article->permalink); $article->save(); } } }); Category::deleted(function ($category) { $categories = Category::where('blog_category_id', '=', $category->blog_category_id)->orderBy('order', 'asc')->get(); $i = 1; foreach ($categories as $category) { $category->order = $i; $i++; $category->save(); } $articles = Article::where('blog_category_id', '=', $category->blog_category_id)->orderBy('order', 'asc')->get(); $i = 1; foreach ($articles as $article) { $article->order = $i; $i++; $article->save(); } }); }