Следующий запрос, построенный с Laravel, не работает (он не возвращает никаких записей):
$this->projectstages = ProjectBuildStage::leftJoin('projects', 'projects.id', '=', 'project_build_stages.project_id')
->leftJoin('build_stages as bs', 'bs.id', '=', 'project_build_stages.build_stage_id')
->leftJoin('build_stage_types as bst', 'bst.build_stage_id', '=', 'bs.id')
->select('bs.build_stage', 'bst.build_stage_type', 'bst.id as build_stage_type_id', 'bs.id')
->where('projects.id', $ProjectId)
->where('bst.project_id',$ProjectId);
if ($buildstageId != 0 && $buildstageId != null && $buildstageId != 'all') {
$this->projectstages->where('bs.id', '=', $buildstageId);
}
$this->projectstages->orderBy('bs.sort_order', 'asc')->orderBy('bst.sort_order', 'asc');
$this->projectstages->get();
К SQL результат:
"select `bs`.`build_stage`, `bst`.`build_stage_type`, `bst`.`id` as `build_stage_type_id`, `bs`.`id` from `project_build_stages` left join `projects` on `projects`.`id` = `project_build_stages`.`project_id` left join `build_stages` as `bs` on `bs`.`id` = `project_build_stages`.`build_stage_id` left join `build_stage_types` as `bst` on `bst`.`build_stage_id` = `bs`.`id` where `projects`.`id` = ? and `bst`.`project_id` = ? and `bs`.`id` = ? and `project_build_stages`.`company_id` = ? order by `bs`.`sort_order` asc, `bst`.`sort_order` asc ◀"
Но, если я уберу if и сделаю запрос в виде одного оператора, он будет работать:
$this->projectstages = ProjectBuildStage::leftJoin('projects', 'projects.id', '=', 'project_build_stages.project_id')
->leftJoin('build_stages as bs', 'bs.id', '=', 'project_build_stages.build_stage_id')
->leftJoin('build_stage_types as bst', 'bst.build_stage_id', '=', 'bs.id')
->select('bs.build_stage', 'bst.build_stage_type', 'bst.id as build_stage_type_id', 'bs.id')
->where('projects.id', $ProjectId)
->where('bst.project_id',$ProjectId)
->orderBy('bs.sort_order', 'asc')
->orderBy('bst.sort_order', 'asc')
->get();
To SQL result:
"select `bs`.`build_stage`, `bst`.`build_stage_type`, `bst`.`id` as `build_stage_type_id`, `bs`.`id` from `project_build_stages` left join `projects` on `projects`.`id` = `project_build_stages`.`project_id` left join `build_stages` as `bs` on `bs`.`id` = `project_build_stages`.`build_stage_id` left join `build_stage_types` as `bst` on `bst`.`build_stage_id` = `bs`.`id` where `projects`.`id` = ? and `bst`.`project_id` = ? and `bs`.`id` = ? and `project_build_stages`.`company_id` = ? order by `bs`.`sort_order` asc, `bst`.`sort_order` asc ◀"
Может кто-нибудь укажите мне, что я делаю неправильно в первом примере? Второй работает просто отлично, но первый не возвращает никаких записей
Спасибо!