简体   繁体   English

如何使用 eloquent 在 Laravel 中编写多个连接

[英]how to write multiple join in laravel using eloquent

I am learning laravel.我正在学习laravel。 I want to write following query in laravel using eloquent:我想使用 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

Above query works perfectly in phpmysql以上查询在 phpmysql 中完美运行

I have Models for each table as follows:我有每个表的模型如下:

Table: branches --- Model: Branch表:分支 --- 型号:分支

Table: branch_work_metadata --- Model: BranchWorkMetadata表:branch_work_metadata --- 模型:BranchWorkMetadata

Table: branch_work_lookup --- Model: BranchWorkLookup表:branch_work_lookup --- 模型:BranchWorkLookup

When I tried to write above query in laravel it gives me error当我尝试在 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();  

Above gives me error以上给了我错误

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`)

Also tried following:还尝试了以下操作:

$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();

giving error给出错误

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`)

Whats going wrong?怎么了? Not able to find solution.无法找到解决方案。 Please help.请帮忙。 Thanks in advance.提前致谢。

You are trying to mix the old school join syntax with the modern version used by Laravel's query builder, which is also the version you should be using.您正在尝试将旧式连接语法与 Laravel 查询构建器使用的现代版本混合使用,这也是您应该使用的版本。 Try this instead:试试这个:

$branch = DB::table('branches as b')
    ->select('b.branch_name', 'w.branch_work_name')
    ->join('branch_work_lookup as bw', 'b.branch_id', '=', 'bw.branch_id')
    ->join('branch_work_metadata as w', 'w.branch_work_id', '=', 'bw.branch_work_id')
    ->get();

To be clear, this is the raw MySQL query you should be using:需要明确的是,这是您应该使用的原始 MySQL 查询:

SELECT
    b.branch_name,
    w.branch_work_name
FROM branches AS b
INNER JOIN branch_work_lookup bw
    ON b.branch_id = bw.branch_id
INNER JOIN branch_work_metadata AS w
    ON w.branch_work_id = bw.branch_work_id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM