Skip to content

Instantly share code, notes, and snippets.

@jlem
Last active September 21, 2016 01:21
Show Gist options
  • Save jlem/2312c7c09f69fa1a93f4a0e15515eda5 to your computer and use it in GitHub Desktop.
Save jlem/2312c7c09f69fa1a93f4a0e15515eda5 to your computer and use it in GitHub Desktop.
Content slugger
<?php
function makeSlug(Entity $entity)
{
return sprintf("%d-%s", $entity->getID(), str_slug($entity->getName()));
}
/*
Entity with ID 3343 and name of "This Is An Article About Code"
becomes 3343-this-is-an-article-about-code
Eloquent can actually take this whole string and use it as the $id when
using `Foo::find($id)` since it type casts to an integer.
This gives you the best of three worlds:
1. Automatic name collision avoidance
2. SEO-friendly URLs
3. Easy and reliable numeric ID lookup
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment