【问题标题】:Laravel model ON methodLaravel 模型 ON 方法
【发布时间】:2021-03-11 20:26:59
【问题描述】:

关系/甚至嵌套关系是否尊重方法?

例如:

User::on('conn1')->with('sports')->get();

$u = User::on('conn1')->where('id', 1)->first();
$u->sports;

【问题讨论】:

标签: php laravel eloquent multi-tenant


【解决方案1】:

是的,“on”方法可用于从不同连接的表中检索数据。 例如。 User::on('conn1')->with('sports')->get();

这仅在表 Sports 属于同一连接时才有效。如果运动属于另一个连接,请确保在 Sports 模型中定义它,如下所示

protected $connection = 'conn2';

【讨论】:

  • 我不能这样做,因为这是多租户应用程序,我需要合并所有租户的数据。我希望框架将在所有使用 with() 的调用关系上使用 on 方法的连接。
【解决方案2】:

如果你不想使用 on() 方法,那么你可以试试这个。

$user = new User;
$user->setConnection('conn1');
$u = $user->where('id', 1)->first();

然后你可以在你的用户模型中设置连接。

class User extends Eloquent {

    protected $connection = 'conn1';

}

但这也很好。

$user = User::on( 'conn1' )->where( 'id', 1 )->first();

【讨论】:

    猜你喜欢
    • 2020-09-11
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    相关资源
    最近更新 更多