Is it possible to join table as below in Laravel?

别来无恙 提交于 2019-12-13 07:57:44

问题


how can I join two tables with multiple columns in laravel, somthing like the below query:

SELECT
    *
FROM
    tab1 AS e
INNER JOIN
    tab2 AS v ON v.Case1 = e.Case1 and v.id = e.id;

I have searched in google but could not find any way.

This is the code I've tried so far:

DB::table('tab1 as e')
    ->join('tab2 as v', 'v.Case1', '=', 'e.Case1', 'and', 'v.id', '=', 'e.id')
    ->get();

回答1:


Have you tried the following?

DB::table('tab1 as e')
->join('tab2 as v', function($join)
{
   $join->on('v.Case1', '=', 'e.Case1');
   $join->on('v.id','=', 'e.id');

})
->get();



回答2:


This is how you have to go around it and it must work. Take this example of mine I did and it works

 $categ =  DB::connection('mysql')->table('SubCategories')
                 ->leftJoin('Categories', 'Categories.id', '=', 'SubCategories.categories_id')
            ->select('SubCategories.id','SubCategories.Code','SubCategories.Name','SubCategories.Description','Categories.Name as CategoryName'
              )
            ->get();

DB::connection('your connection from app.php file')table('tab1 as e')
    ->join('tab2 as v', 'v.Case1', '=', 'e.Case1', 'and', 'v.id', '=', 'e.id')
    ->Select('your select statement'),
    ->get();


来源:https://stackoverflow.com/questions/53457659/is-it-possible-to-join-table-as-below-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!