Skip to content
How to use Laravel Form Requests to Validate Path and Query Parameters
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
app
bootstrap
config
database
public
resources
routes
storage
tests
.editorconfig
.env
.env.example
.gitattributes
.gitignore
artisan
composer.json
composer.lock
package.json
phpunit.xml
readme.md
server.php
webpack.mix.js

readme.md

Laravel Validate Query and Path/Route Parameters

An example repository on how to validate route/path and query parameters using Laravel Form Requests.

The two primary classes at play are here

Read more about how to easily use this in this snippet: https://commandz.io/snippets/laravel/laravel-validate-path-or-query-parameter-in-form-request/

<?php

protected $routeParametersToValidate = ['post_id' => 'postId'];
protected $queryParametersToValidate = ['include' => 'include'];

public function rules()
{
    return [
        'post_id' => 'required|exists:posts,id',
        'include' => 'sometimes|numeric|nullable',
        'name' => 'required|string',
    ];
}

public function all($keys = null)
{
    $data = parent::all();

    foreach ($this->routeParametersToValidate as $validationDataKey => $routeParameter) {
        $data[$validationDataKey] = $this->route($routeParameter);
    }

    foreach ($this->queryParametersToValidate as $validationDataKey => $queryParameter) {
        $data[$validationDataKey] = $this->query($queryParameter);
    }

    return $data;
}
You can’t perform that action at this time.