Skip to content

Instantly share code, notes, and snippets.

@jesseleite
Created July 28, 2015 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesseleite/fb96ba0f47f74f9e83e6 to your computer and use it in GitHub Desktop.
Save jesseleite/fb96ba0f47f74f9e83e6 to your computer and use it in GitHub Desktop.
Slugify using Model observer.
Job::creating(function ($object) {
$slug = str_slug($object->title);
$exists = $object->where('title_slug', $slug)->orWhere('title_slug', 'like', $slug.'-%')->get();
$object->title_slug = $exists->count() == 0 ? $slug : $slug . '-' . $exists->count();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment