fredyounan
1/6/2014 - 1:18 AM

Generating unique URL slugs (Laravel) I'm generating unique slugs with minimal code and database queries the following way: http://forums.la

Generating unique URL slugs (Laravel) I'm generating unique slugs with minimal code and database queries the following way: http://forums.laravel.io/viewtopic.php?id=6629

<?php

// Generating unique URL slugs (Laravel)
// http://forums.laravel.io/viewtopic.php?id=6629
// I'm generating unique slugs with minimal code and database queries the following way:

private function getEventSlug($title)
{
    $slug = Str::slug($title);
    $slugCount = count( $this->event->whereRaw("slug REGEXP '^{$slug}(-[0-9]*)?$'")->get() );

    return ($slugCount > 0) ? "{$slug}-{$slugCount}" : $slug;
}