Hello!
I'm encountering an issue with form validation and user storage. I have two database connections, one for the front-office, one for the back-office (two schemas, 'fo' and 'bo', in postgresql).
My relation 'admin' is in my 'bo' schema. I created a model that looks like that:
class Admin extends Eloquent implements UserInterface {
protected $connection = 'bo';
protected $table = 'admin';
public static $rules = array(
'email' => 'required|email|unique:admin'
// ...some other rules
);
Then if I want to add an admin, here's my 'store' method in my controller:
$rules = Admin::$rules;
$validator = Validator::make(Input::all(), $rules);
I get an error :
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "admin" does not exist LINE 1: select count(*) as aggregate from "admin" where "email" = $1 ^ (SQL: select count(*) as aggregate from "admin" where "email" = test@test.com)
If I delete the rule about email uniqueness, it works. Do rules not take into account the $connection from my model? Is there a way to make it work?
Thank you
You probably just need to tell the Validator
what connection you are using. This can be done using the setPresenceVerifier
method. Here is a code example:
// Create a new instance of the presence verifier. Let's make the IoC do the work.
$verifier = App::make('validation.presence');
// Set the connection on the verifier.
$verifier->setConnection($connection);
// Create the validator instance (your code)
$rules = Admin::$rules;
$validator = Validator::make(Input::all(), $rules);
// Set the validator's presence verifier.
$validator->setPresenceVerifier($verifier);
In the above code, $connection
is the connection/schema name (fo
or bo
). Good luck!
Here is my solution for laravel 5:
namespace App\Model;
use Illuminate\Validation\PresenceVerifierInterface;
class NewPresenceVerifier implements PresenceVerifierInterface {
// Override required methods here
}
namespace App\Providers;
use App\Model\NewPresenceVerifier;
use Illuminate\Validation\ValidationServiceProvider as BaseValidationServiceProvider;
class ValidationServiceProvider extends BaseValidationServiceProvider {
protected function registerPresenceVerifier()
{
$this->app->singleton('validation.presence', function($app)
{
return new NewPresenceVerifier();
});
}
}
'App\Providers\ValidationServiceProvider'
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community