laravel 中with关联查询限定查询字段

40次阅读
没有评论

1.关联加入select指定字段

public function CustomerInfo()
{
return $this->belongsTo(CustomerInfo::class, ‘customer_id’, ‘id’)->select([‘id’, ‘company_name’, ‘company_code’, ‘external_code’, ‘credit_total’, ‘customer_level’, ‘customer_state’, ‘goods_state’]);
}

  1. 闭包内select指定字段

ActiveJieSuan::with([‘user’ => function ($query) {$query->select(‘id’,’name’);},
‘active’=> function ($query) {
$query->select(‘id’,’name’,’start’);
}
])
->paginate()->toArray();
3.简便写法

ActiveJieSuan::with([‘user:id,name’,’active:id,name,start’])
->paginate()
->toArray();

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 
评论(没有评论)