Created
November 30, 2017 20:49
-
-
Save azimidev/0d404c1aec7e06e1fd3df47556f07f74 to your computer and use it in GitHub Desktop.
Laravel Slug Attribute Simple and Best
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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