How to do Multiple Database User Validation in Laravel?

Validating user data is one important aspect of application development. There are separate rules for different values. For example, mobile numbersmaximum character validations, and image and video uploads. Similarly, we can also define multiple database user validation rules in Laravel.

We can create Laravel validation rules in the Controller class. The rules check values before we save user data in the application and database. Validation rules can be applied to many types of inputs from a user in Laravel. Let’s see how we use them on user credentials in multiple databases.

Multiple Database User Validation

We can add these validation rules in an array variable, as shown below.

public static	$rules = array(
     'username'         => 'required|max:40|unique:connection_name.user',
     'name'             => 'sometimes|required',
     'email'            => 'required|email|max:255|unique:connection_name.user',
     'password'         => 'sometimes|required|confirmed|min:6',
     'password_current' => 'sometimes|required'
);

We can add multiple rules for database credentials, and user types. For example unique:connection_name.user above tells that email for connection name ‘user’ should be unique.

The. connection_name corresponds to a specific connection of the ‘multiple databases’. Therefore, it validates the user entry for that specific database’s user credentials.

When working with multiple databases, the rules should mention the database or connection name to apply to users for that. specific connection.

Since the rule is embedded in a static variable, we define this rule in a Modal, so it can be used. throughout the application.

Leave a Comment