Skip to content

Instantly share code, notes, and snippets.

@xtrasmal
Created April 2, 2018 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtrasmal/1f92eb6da3a0be4c9ab39e11dc6d4dac to your computer and use it in GitHub Desktop.
Save xtrasmal/1f92eb6da3a0be4c9ab39e11dc6d4dac to your computer and use it in GitHub Desktop.
Guarded FormRequest
<?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