Created
April 2, 2018 01:38
-
-
Save xtrasmal/1f92eb6da3a0be4c9ab39e11dc6d4dac to your computer and use it in GitHub Desktop.
Guarded FormRequest
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 | |
/** | |
* Trait Guarded | |
* use in a Laravel FormRequest class | |
*/ | |
trait Guarded | |
{ | |
/** | |
* Get all input parameter that are guarded by and | |
* match the rules array of the form request | |
* | |
* @return mixed | |
*/ | |
public function guarded() | |
{ | |
return $this->all(); | |
} | |
/** | |
* We extend the all() method of the parent, | |
* without breaking current code using it | |
* | |
* @param null $keys | |
* @return mixed | |
*/ | |
public function all($keys = null) | |
{ | |
$guarded = array_keys($this->rules()); | |
$merged = array_merge($keys ?? [], $guarded); | |
return parent::all(array_unique($merged)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment