I ran into an interesting bit of fun today where I had some rules in a Laravel 4 application that were checking the database table for unique values. The issue is that I had the tables in this app in a separate database that is not currently set as the default connection on the Laravel 5 monolith that it is being integrated into.

A bit of background

Matt Staufer has a great intro series on the changes to Laravel in version 5 on his blog, and includes a post about the basics of FormRequest objects for injected validation.

What can we do?

The trick was to override the function found in Illuminate\Foundation\Http\FormRequest called getValidatorInstance() in your FormRequest object. You can then inject a new Illuminate\Validation\DatabasePresenceVerifier with the specified connection.

protected function getValidatorInstance()
{
    $factory = $this->container
    	->make('Illuminate\Validation\Factory');
    
    $verifier = $this->container
    	->make('validation.presence');

    $verifier->setConnection('connectionName');

    $factory->setPresenceVerifier($verifier);

    if (method_exists($this, 'validator'))
    {
        return $this->container
          ->call(
              [$this, 'validator'], 
              compact('factory')
          );
    }

    return $factory->make(
        $this->all(), 
        $this->container
        	->call([$this, 'rules']), 
        $this->messages(), 
        $this->attributes()
    );
}