У меня есть приложение Laravel 6. Я хочу использовать eloquent для сбора только тех сотрудников, чей уровень занятости равен true.
Вопрос в том, как применить предложение where
к результатам функции avg
без второго выбора в Eloqunt. ? Например, что-то вроде этого:
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`
Но проблема в том, что engaged
в состоянии WHERE здесь не распознано и MySQL показывает:
Неизвестный столбец «занят» в 'предложении 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');
});
}
И предложение where применяется следующим образом:
public function filterEngaged(EloquentBuilder $query, $method, $clauseOperator, $value, $column, $table)
{
call_user_func([$query, $method], $column, $value);
// $method = 'where', $column = 'engaged' , $value = 1
}