我有两个模型。其中之一Sales具有以下属性:protected $visible = [ 'id', 'client_id', 'is_paid'];public function client(){ return $this->belongsTo(Client::class, 'client_id', 'id');}第二个是Client:protected $visible = [ 'id', 'name'];public function sales(){ return $this->hasMany(Sales::class, 'client_id', 'id');}我以这种方式请求数据:$getSalewithClient = Sales::where('id', "=", 1)->with(["client"])->get()->toArray并且只从销售中获取数据:{ id: 1, client_id: 1, is_paid: null }但是缺少客户信息,甚至没有列出密钥。我不知道为什么,因为起初这是工作,但突然停止工作,我很绝望。
2 回答

杨__羊羊
TA贡献1943条经验 获得超7个赞
因为你使用$visible的是,关系属性是隐藏的,你需要将它添加到$visible数组中:
protected $visible = [
'id',
'client_id',
'is_paid',
'client' // add relationship name here
];
这样你就可以看到它的结果。

慕雪6442864
TA贡献1812条经验 获得超5个赞
return $this->belongsTo(Client::class, 'client_id', 'id');
我认为应该是
return $this->belongsTo(Client::class, 'id', 'client_id');
第一个参数是外表上的列,第二个参数是当前表上的列。
- 2 回答
- 0 关注
- 152 浏览
添加回答
举报
0/150
提交
取消