/** * @param $slug */ public function setSlugAttribute($slug) { if (empty($slug)) { $this->slug = Str::slug_utf8($this->attributes['title']); $slug = Article::where('slug', $this->slug)->pluck('slug'); $this->attributes['slug'] = empty($slug) ? $this->slug : $this->slug . '-' . str_random(5); } else { if (isset($_REQUEST['_method']) == 'PATCH') { $this->slug = Str::slug_utf8($slug); $slug = Article::where('slug', $this->slug)->count(); $this->attributes['slug'] = $slug <= 1 ? $this->slug : $this->slug . '-' . str_random(5); } else { $this->slug = Str::slug_utf8($slug); $slug = Article::where('slug', $this->slug)->pluck('slug'); $this->attributes['slug'] = empty($slug) ? $this->slug : $this->slug . '-' . str_random(5); } } }