Skip to content

Instantly share code, notes, and snippets.

@mariapaulinar
Created July 6, 2018 16:35
Show Gist options
  • Save mariapaulinar/4c7339bbeac9f7ed1d662fa0ed05e2e9 to your computer and use it in GitHub Desktop.
Save mariapaulinar/4c7339bbeac9f7ed1d662fa0ed05e2e9 to your computer and use it in GitHub Desktop.
Genera recursivamente slug para uri amigable. Ejemplo con una tabla de productos.
<?php
/**
* Genera slug para el producto.
*
* @author María Paulina Ramírez Vásquez <maria.paulina.r.v@gmail.com>
* @param string $descripcion Descripción del producto.
*
* @return string
*/
public static function generarSlug($descripcion, $i = 0)
{
$slug = ($i > 0) ? str_slug($descripcion . '-' . $i) : str_slug($descripcion);
$count_prods = DB::table('productos')->where('slug', $slug)->count();
if($count_prods > 0) {
$i++;
return self::generarSlug($descripcion, $i);
}
return $slug;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment