Laravel 嵌套跨数据库关系
Laravel nested cross database relation
为什么设置连接在嵌套关系中不起作用?
Follower.php
class Follower extends Model {
$connection = 'followers';
public function details() {
return $this->belongsTo(User::class, 'user_id');
}
}
User.php
class User extends Model {
$connection = 'users';
protected $withCount = ['notifications'];
public function notifications() {
return $this->setConnection('followers')->hasMany('App\Models\Notifications');
}
}
和查询:
Follower::query()->where('user_id', 1)->with('details')->get();
它抛出:
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'users.notifications' doesn't exist (SQL: select ` ....
但是当我尝试这个的时候效果很好
User::with('notifications')->find(1);
更新
Notification.php
class Notification extends Model
{
protected $fillable = [
'user_id',
'builder',
'notification_type',
'comment_id',
'read_at'
];
}
这是自 2018 年以来 laravel 的一个已知问题,有关详细信息,请参阅此 thread , also there is a open pull request 以解决此问题,并将在下一个版本之前得到修复
现在您可以使用 hoyvoy/laravel-cross-database-subqueries 包
使用这个安装包
composer require hoyvoy/laravel-cross-database-subqueries
Follower.php
use Hoyvoy\CrossDatabase\Eloquent\Model;
class Follower extends Model {
$connection = 'followers';
public function details() {
return $this->belongsTo(User::class, 'user_id');
}
}
User.php
use Hoyvoy\CrossDatabase\Eloquent\Model;
class User extends Model {
$connection = 'users';
protected $withCount = ['notifications'];
public function notifications() {
return $this->setConnection('followers')->hasMany('App\Models\Notifications');
}
}
为每个模型添加默认值 protected $connection
为什么设置连接在嵌套关系中不起作用?
Follower.php
class Follower extends Model {
$connection = 'followers';
public function details() {
return $this->belongsTo(User::class, 'user_id');
}
}
User.php
class User extends Model {
$connection = 'users';
protected $withCount = ['notifications'];
public function notifications() {
return $this->setConnection('followers')->hasMany('App\Models\Notifications');
}
}
和查询:
Follower::query()->where('user_id', 1)->with('details')->get();
它抛出:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'users.notifications' doesn't exist (SQL: select ` ....
但是当我尝试这个的时候效果很好
User::with('notifications')->find(1);
更新 Notification.php
class Notification extends Model
{
protected $fillable = [
'user_id',
'builder',
'notification_type',
'comment_id',
'read_at'
];
}
这是自 2018 年以来 laravel 的一个已知问题,有关详细信息,请参阅此 thread , also there is a open pull request 以解决此问题,并将在下一个版本之前得到修复
现在您可以使用 hoyvoy/laravel-cross-database-subqueries 包
使用这个安装包
composer require hoyvoy/laravel-cross-database-subqueries
Follower.php
use Hoyvoy\CrossDatabase\Eloquent\Model;
class Follower extends Model {
$connection = 'followers';
public function details() {
return $this->belongsTo(User::class, 'user_id');
}
}
User.php
use Hoyvoy\CrossDatabase\Eloquent\Model;
class User extends Model {
$connection = 'users';
protected $withCount = ['notifications'];
public function notifications() {
return $this->setConnection('followers')->hasMany('App\Models\Notifications');
}
}
为每个模型添加默认值 protected $connection