Created
July 6, 2018 16:35
-
-
Save mariapaulinar/4c7339bbeac9f7ed1d662fa0ed05e2e9 to your computer and use it in GitHub Desktop.
Genera recursivamente slug para uri amigable. Ejemplo con una tabla de productos.
This file contains 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 | |
/** | |
* 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