Last active
September 21, 2016 01:21
-
-
Save jlem/2312c7c09f69fa1a93f4a0e15515eda5 to your computer and use it in GitHub Desktop.
Content slugger
This file contains hidden or 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 | |
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