Skip to content

Instantly share code, notes, and snippets.

@azimidev
Created November 30, 2017 20:49
Show Gist options
  • Save azimidev/0d404c1aec7e06e1fd3df47556f07f74 to your computer and use it in GitHub Desktop.
Save azimidev/0d404c1aec7e06e1fd3df47556f07f74 to your computer and use it in GitHub Desktop.
Laravel Slug Attribute Simple and Best
<?php
public function setSlugAttribute($value)
{
$slug = str_slug($value);
$original = $slug;
$count = 2;
while (static::whereSlug($slug)->exists()) {
$slug = "{$original}-" . $count++;
}
$this->attributes['slug'] = $slug;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment