Subscribe to PHP Freaks RSS

Laravel: Validate GET parameters

syndicated from planet-php.net on September 25, 2018

In one of our Laravel 5.6 applications at work I had to validate the data of an incoming POST request to our API.

Validation of POST data is easily done with custom Request classes that provide their validation checks via the rules() method.

A different issue are GET parameters that we got via our route definition, because the Request objects only validate POST data.

My solution was to manually add the relevant GET parameters to array-of-data-to-validate by overwriting the all() method:

app/Http/Requests/SomethingWithAType.php
    /**
     * Inject GET parameter "type" into validation data
     *
     * @param array $keys Properties to only return
     *
     * @return array
     */
    public function all($keys = null)
    {
        $data = parent::all($keys);
        $data['type'] = $this->route('type');
        return $data;
    }