Laravel即时通过自定义租户连接请求验证 [英] Laravel request validation with custom tenant connection on-the-fly
问题描述
我正在创建具有多个数据库的SaaS应用程序,并且根据登录的用户即时设置与数据库的连接.
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.
我现在的问题是同时使用$request->validate
函数和unique
和exists
函数.
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'
但这迫使我在config\database.php
文件中创建我的连接,而这是我不想做的事情.
But this forces me to create my connections within the config\database.php
file and that's something I don't want to do.
当前,如果我这样做:
$request->validate(['email' => 'required|unique:clients,email']);
我看到一个错误:
找不到基本表或视图:1146表 'mydb_admin.clients'
Base table or view not found: 1146 Table 'mydb_admin.clients'
这是正确的,因为我的管理数据库没有表clients
.那么,在已有系统的情况下,如何在子数据库中使用$request->validate
函数?
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?
推荐答案
您可以这样做,因为您已经在中间件中设置了tenant
连接:
You can do it like this since you are already setting the tenant
connection in your middleware:
$request->validate([
'email' => 'required|unique:tenant.clients'
]);
这篇关于Laravel即时通过自定义租户连接请求验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!