我想在块中使用雄辩的列名。我已经尝试过relationFunctionName.colun_name
和tableName.column_name
,但它给了我一个错误。不存在这样的列。 我的查询:
$d = FormsValues::select('forms_values.id', 'forms_values.patient_id','forms_values.doctor_id')
// ->with('getUserPatientAssinged')
->with(array('getUserPatientAssinged' => function($query) use($user) {
$query
->select('form_value_id','consultation_assigned_by','date as consultation_date', 'start_time as consultation_start_time', 'end_time as consultation_end_time','doctor_id')
}));
-其他代码---
$d = $d->where('getUserPatientAssinged.doctor_id', Auth::user()->id);
任何解决方案? 谢谢
通常,除非您也使用->with()
,否则不能使用->join()
子句之外的关系的列。例如:
FormsValues::with(['getUserPatientAssinged' => function ($query) { ... }])
->where('assigned_patients.doctor_id', auth()->user()->id)
->get();
注意:假设有一些表名,因为要引用它而不是关系名
上面的代码很可能会在assigned_patients.doctor_id
周围抛出错误,因为该列在当前作用域中不可用,该列是未知列。这可以通过使用->join()
和->with()
子句来解决:
FormsValues::with(['getUserPatientAssinged' => function ($query) { ... }])
->join('assigned_patients', ...)
->where('assigned_patients.doctor_id', ...)
->get();
或者,只要建立了关系,我们就可以使用->whereHas()
。为此,我们正在寻找“分配的患者”的Doctor
,因此:
FormsValues::with(['getUserPatientAssinged' => function ($query) { ... }])
->whereHas('getUserPatientAssigned.doctor', function ($query) {
return $query->where('id', auth()->user()->id);
})->get();