1

I'm creating an SaaS application with multiple databases and the connection to the database is set on-the-fly according to the user logged in. You can see my problem and solution here back when I was trying to figure out how to do it.

My problem now is using the $request->validate function with both unique and exists functions.

The documentation says that we can set a Custom Database Connection, by doing something like:

'email' => 'unique:connection.clients,email'

But this forces me to create my connections within the config\database.php file and that's something I don't want to do.

Currently, if I do:

$request->validate(['email' => 'required|unique:clients,email']);

I got an error saying:

Base table or view not found: 1146 Table 'mydb_admin.clients'

Which is correct, since my Admin database does not have the table clients. So how can I, with the system I have in place, use the $request->validate function with my child databases?

Linesofcode
  • 5,327
  • 13
  • 62
  • 116

1 Answers1

0

You can do it like this since you are already setting the tenant connection in your middleware:

$request->validate([
    'email' => 'required|unique:tenant.clients'
]);
Chin Leung
  • 14,621
  • 3
  • 34
  • 58