Skip to content

Instantly share code, notes, and snippets.

@robertdrakedennis
Created October 27, 2019 17:56
Show Gist options
  • Save robertdrakedennis/e9058d5e66e90a3c585c117216dc2dfd to your computer and use it in GitHub Desktop.
Save robertdrakedennis/e9058d5e66e90a3c585c117216dc2dfd to your computer and use it in GitHub Desktop.
Generating and attaching slug to array.
<?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;
}
}
@robertdrakedennis
Copy link
Author

this is mainly for form requests and goes hand in hand with my other helpers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment