如何在 $query 中传递 _user_id 列值。这是hasMany的关系。我无法弄清楚如何将 RFX 的用户 user_id 放入 $query where 条件中。
public function response_pricings(){
return $this->hasMany('App\Models\Website\RFXRequestPricingResponse', ['rfx_request_id'=>'_rfx_request_id', 'user_id'=>'_user_id'])->selectRaw("*");
}
return RFXRequestSupplierResponded::select(
'id as _id',
'rfx_request_id as _rfx_request_id',
'user_id as _user_id',
'status',
'is_qualified',
DB::RAW('(SELECT name FROM users WHERE id=user_id) as user_name'),
DB::RAW('(SELECT note FROM rfx_request_response_notes WHERE rfx_request_id='.$rfx_request_id.' AND user_id=rfx_request_suppliers_responded.user_id LIMIT 1) as note')
)
->with(
[
'response_pricings' => function ($query) {
/*$query->where('user_id', $_user_id);*/
}
]
)
->where('rfx_request_id',$rfx_request_id)
->get();
最佳答案
当您在模型上定义关系时,Laravel 会使用动态确定的外键或您指定的外键自动链接模型。因此,您无需将 user_id
传递给查询。 Laravel 会自动使用 RFXRequestSupplierResponded
实例的 user_id
。
但是,看起来您正在使用多个外键将 RFXRequestSupplierResponded
链接到 RFXRequestPricingResponse
模型。 Eloquent 对此没有内置支持。查看 this question 的答案有关如何添加对多个外键的支持的示例。
关于php - 如何在 Eloquent 子查询中传递父列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48320463/