Laravel 5.4'join'查询

时间:2018-11-12 11:08:42

标签: laravel join laravel-5.4 query-builder laravel-query-builder

public function scopeGetCompanyInfoWithBank($query, $comCode) 
{
    $query->select(
        DB::raw("CONCAT(rtrim(COM_NAME), '') AS COM_NAME"),
        DB::raw("CONCAT(rtrim(COM_ADD1), '') AS COM_ADD1"),
        DB::raw("CONCAT(rtrim(COM_ADD2), '') AS COM_ADD2"),
        DB::raw("CONCAT(rtrim(COM_ADD3), '') AS COM_ADD3"),
        DB::raw("CONCAT(rtrim(COM_ADD4), '') AS COM_ADD4"),
        DB::raw("CONCAT(rtrim(COM_BNKCODE), '') AS COM_BNKCODE"),
        DB::raw("CONCAT(rtrim(BANK_NAME), '') AS BANK_NAME") // NOT WORKING.
    )
    ->join("bank", function ($join) {
        $join->on('bank.BANK_CODE', "=", 'COM_BNKCODE');
    })
    ->where('COM_CODE', '=', $comCode)
    ->get();
}
  • 2个表:“公司”和“银行”。
  • “ COM_CODE”:“公司”的主键。
  • “ BANK_CODE”:“银行”的主键。
  • “ COM_BNKCODE”:“ company”的外键=“ BANK_CODE”。

问题:
无法从上述查询中获取“ BANK_NAME”。

1 个答案:

答案 0 :(得分:-1)

希望这会对您有所帮助

公共函数scopeGetCompanyInfoWithBank($ query,$ comCode) {     $ this-> data ['query'] = company :: join('bank','BANK_CODE','=','company.COM_BNKCODE')                            ->选择(“ company.COM_NAME”,“ company.COM_ADD1”,                             'company.COM_ADD2','company.COM_ADD3',                             'company.COM_ADD4','company.COM_BNKCODE')                            ->哪里('company.COM_CODE','=',$ comCode)                            ->获取(); }