Skip to content

Request::getFiltered vs fatal errors

macropay-solutions edited this page Jun 4, 2025 · 1 revision

laravel-crud-wizard-free Request::getFiltered

This Request macro: getFiltered takes advantage of the Symfony’s ParameterBag methods:

Request::macro(
    'getFiltered',
    /**
     * @param string|int $filter can be 'alpha', 'alnum', 'digits' or an int flag
     * @see ParameterBag::filter(), \filter_var() for $filter and $options
     * @see ParameterBag::getAlpha()
     * @see ParameterBag::getAlnum()
     * @see ParameterBag::getDigits()
     * @see ParameterBag::getInt()
     * @see ParameterBag::getBoolean()
     */
    function (
        string $key,
        mixed $default = null,
        string|int $filter = \FILTER_DEFAULT,
        array|int $options = [],
    ): mixed {
    ...
  }
);

Examples:

(string)\request('signature', '');

The above will throw Array to string conversion error if signature is array.

The proper way of handling it before:

(string)\filter_var(\request('signature', ''));

The new way of handling it:

(string)\request()->getFiltered('signature', '');

For go to source and type hinting, you can put in a child of a Request from your project:

/**
 * @method getFiltered(string $key, mixed $default = null, string|int $filter = FILTER_DEFAULT, int|array $options = [])
 */
class ChildRequest extends Request

NOTE

getFiltered will not return null but instead it will return empty string in this case:

$request->getFiltered('key');
Clone this wiki locally