框架:laravel 5.4
表: pq_coupon
pq_picture
关系:根据pq_coupon 表中 picture_id 字段,查找出pq_picture的picture_url字段
代码1:
(控制器)
$data = $coupon
->with(['prcture'=>function($query){
$query->select('picture_url');
}])
->where('id','>=',1)->limit(2)->select('id','note')->get();
(models)
function prcture(){
return $this->belongsTo(\App\Models\Picture::class,'picture_id','id');
}
查询失败,picture整个为空
代码2:
(控制器)
$data = $coupon->with('prcture')->where('id','>=',1)->limit(2)->select('id','note')->get();
(models)
function prcture(){
// belongsTo 多对一(从属)
return $this->belongsTo(\App\Models\Picture::class,'picture_id','id')->select('picture_url');
}
结果同上,都是查询picture为null
求助:如何让with()指定prcture 只查找prcture_url字段?这两种方法都不行,是版本问题?
www说
暮色呼如