我正在学习laravel。我想使用 eloquent 在 laravel 中编写以下查询:
select b.branch_name, w.branch_work_name from branches AS b, branch_work_metadata AS w, branch_work_lookup AS bw where b.branch_id = bw.branch_id AND w.branch_work_id = bw.branch_work_id
上面的查询在 phpmysql 中完美运行
我有每个表的模型如下:
表:分支机构 --- 型号:分支机构
表:branch_work_metadata --- 模型:BranchWorkMetadata
表:branch_work_lookup --- 模型:BranchWorkLookup
当我尝试在 laravel 中编写上述查询时,它给了我错误
$branch = DB::table('branches as b', 'branch_work_metadata as w', 'branch_work_lookup as bw')
->select('b.branch_name','w.branch_work_name')
->join('b.branch_id','=','bw.branch_id')
->join('w.branch_work_id','=','bw.branch_work_id')
->get();
上面给了我错误
Illuminate\Database\QueryException
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'b.branch_id' doesn't exist (SQL: select `b`.`branch_name`, `w`.`branch_work_name` from `branches` as `b` inner join `b`.`branch_id` on `=` = `bw`.`branch_id` inner join `w`.`branch_work_id` on `=` = `bw`.`branch_work_id`)
还尝试了以下操作:
$branch = BranchWorkLookup::Join('branches.branch_id','=','branch_work_lookup.branch_id')
->Join('branch_work_metadata.branch_work_id','=','branch_work_lookup.branch_work_id')
->select(
'branches.branch_name',
'branch_work_name'
)
->get();
给出错误
Illuminate\Database\QueryException
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'branches.branch_id' doesn't exist (SQL: select `branches`.`branch_name`, `branch_work_name` from `branch_work_lookup` inner join `branches`.`branch_id` on `=` = `branch_work_lookup`.`branch_id` inner join `branch_work_metadata`.`branch_work_id` on `=` = `branch_work_lookup`.`branch_work_id`)
怎么了?无法找到解决方案。请帮忙。提前致谢。
肥皂起泡泡