Laravel 5 Form request validation with parameters

For those who switched to laravel 5 :

public function rules()
{
    $business = $this->route('business');
    // rest of the code
}

Lets say this is your model binding:

$router->model('business', 'App\Business');

Then you can reference the Business class from within the FormRequest object like this:

public function rules()
{
    $business = $this->route()->getParameter('business');
    // rest of the code
}

Note that if you use your form request both for create and update validation, while creating the record, the business variable will be null because your object does not exists yet. So take care to make the needed checks before referencing the object properties or methods.


There can be many ways to achieve this. I do it as below.

You can have a hidden field 'id' in your business form like bellow,

{!! Form::hidden('id', $business->id) !!}

and you can retrieve this id in FormRequest as below,

public function rules()
{
    $businessId = $this->input('id');

    return [
        'name' => 'required|unique:businesses,name,'.$businessId,
        'url' => 'required|url|unique:businesses'
    ];
}