Skip to content

Instantly share code, notes, and snippets.

@jenky
Created June 10, 2019 11:34
Show Gist options
  • Save jenky/1eb4c126a1f831229da03cf5bcaad954 to your computer and use it in GitHub Desktop.
Save jenky/1eb4c126a1f831229da03cf5bcaad954 to your computer and use it in GitHub Desktop.
ExplodesFormRequestData trait
<?php
namespace App\Support\Traits;
use Illuminate\Support\Str;
trait ExplodesFormRequestData
{
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation()
{
$mergeData = [];
foreach ($this->getExplodable() as $key) {
$input = $this->input($key);
if (is_array($input)) {
continue;
}
if (Str::contains($key, ':')) {
[$key, $delimiter] = explode(':', $key);
} else {
$delimiter = ',';
}
$mergeData[$key] = explode($delimiter, $input);
}
$this->merge($mergeData);
}
/**
* Get all explodable keys.
*
* @return array
*/
protected function getExplodable()
{
return property_exists($this, 'explodable') ? $this->explodable : [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment