我有一个Laravel 6应用程序。我想用雄辩的方式只收集那些敬业度得分等于true的员工。
问题是,如何在函数结果上应用子句而不在 Eloqunt 中进行第二次选择?例如,像这样:whereavg
SELECT
`employees`.*,
AVG(responses.text_answer) > 4.6 AS engaged
FROM
`employees`
INNER JOIN `responses` AS `responses`
ON
`responses`.`employee_id` = `employees`.`id`
INNER JOIN `survey_questions` AS `surveyQuestions`
ON
`surveyQuestions`.`id` = `responses`.`survey_question_id`
INNER JOIN `questions` AS `questions`
ON
`questions`.`id` = `surveyQuestions`.`question_id`
WHERE
engaged = 1 AND `questions`.`question_type_id` = '6S'
GROUP BY
`employees`.`id`
但问题是这里的WHERE条件无法识别,MySQL显示:engaged
“where 子句”中的“已参与”列未知
我雄辩的一部分陈述是这样的:
public function engaged()
{
return $this->relatedToMany(QuestionType::class, function(Builder $query){
if(!$this->joined($query, 'responses')){
$query->join(
'responses AS responses',
'responses.employee_id',
'=',
'employees.id',
'inner'
);
}
if(!$this->joined($query, 'survey_questions')){
$query->join(
'survey_questions AS surveyQuestions',
'surveyQuestions.id',
'=',
'responses.survey_question_id',
'inner'
);
}
if(!$this->joined($query, 'questions')){
$query->join(
'questions AS questions',
'questions.id',
'=',
'surveyQuestions.question_id',
'inner'
);
}
$query->where('questions.question_type_id', '6S');
$query->select('employees.*', \DB::raw('AVG(`responses`.`text_answer`) >= 4.6 AS `engaged`'));
$query->groupBy('employees.id');
});
}
qq_花开花谢_0