laravel - 调用未定义的关系 - Laravel
问题描述
我正在使用最新Laravel
的 . 我在控制器中也有从关系表中选择表和数据的方法:
$cars= Car::with(array('car_photos'=>function($query){
$query->select('id');
}))->get();
Cars
有很多照片。汽车模型:
public function photos(){
return $this->hasMany('App\Models\CarPhoto');
}
CarPhoto
模型:
public function car(){
return $this->hasOne('App\Models\Car');
}
我可以选择汽车的所有照片:
$car= Car::where('id', $id)->first();
$photos = $car->photos;
但是$cars= Car::with....
,我得到:
调用模型 [App\Models\Car] 上的未定义关系 [car_photos]。
解决方案
关系名称不是类名称:
$cars= Car::with(array('photos'=>function($query){
$query->select('id');
}))->get();
推荐阅读
- python - PyQt5:在 Qmovie 中重新加载 gif
- java - 在java中将逗号分隔的字符串转换为json
- android - 滚动选项卡视图中的片段时调用的确切方法
- javascript - 正确处理异步/等待中的错误
- javascript - JavaScript:块方法
- laravel - Laravel XML 站点地图、位置、共享主机
- android - CardView NullPointerException onClickListener
- javascript - 如何在 Vuejs 的 v-for 循环中为多个元素设置动画
- hyperledger - 如何将在 Hyperledger Composer 上开发的应用程序投入生产?
- node.js - 如何显示所有客户端,包括连接到 socket.io 房间的发件人?