Created
June 10, 2019 11:34
-
-
Save jenky/1eb4c126a1f831229da03cf5bcaad954 to your computer and use it in GitHub Desktop.
ExplodesFormRequestData trait
This file contains hidden or 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 | |
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