Created
October 27, 2019 17:56
-
-
Save robertdrakedennis/e9058d5e66e90a3c585c117216dc2dfd to your computer and use it in GitHub Desktop.
Generating and attaching slug to array.
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 | |
if (! function_exists('generate_slug')) { | |
/** | |
* @param array $data | |
* @param string $key | |
* @param bool $unqiue | |
* @return array | |
* @throws Exception | |
*/ | |
function generate_slug(array $data, string $key, bool $unqiue = false): array | |
{ | |
$slug = \Str::slug( | |
\Arr::get($data, $key) | |
); | |
if ($unqiue === true){ | |
$slug = randomize(8, 16, 20) . '-' . $slug; | |
} | |
$data = \Arr::add($data, 'slug', $slug); | |
return $data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is mainly for form requests and goes hand in hand with my other helpers.