【发布时间】:2020-08-03 17:57:37
【问题描述】:
为什么设置连接在嵌套关系中不起作用?
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]:未找到基表或视图:1146 表 'users.notifications' 不存在(SQL: select ` ....
但是当我尝试这个时效果很好
User::with('notifications')->find(1);
更新 Notification.php
class Notification extends Model
{
protected $fillable = [
'user_id',
'builder',
'notification_type',
'comment_id',
'read_at'
];
}
【问题讨论】:
-
user.php 已更新
-
@iamab.in 结果还是一样
-
您的 details 方法在 user_id 中缺少关闭引号,这是 stackoverflow 或代码中的错字吗??
-
@bhucho,抱歉,代码更新了,这是 stackoverflow 上的一个错误
-
你能添加
App\Models\Notifications类吗?
标签: laravel eloquent relationship relation