将连接查询更改为laravel

时间:2018-01-11 06:08:32

标签: php mysql laravel join

我有一个mysql查询

SELECT a.`head_id` AS "Headid",a.`name` AS "HeadName", b.`id` AS "Emp_ID",b.`name` AS "Emp_name" FROM users a join users b ON a.id=b.head_id where a.`head_id`=63 or b.`id`=63

如何将此查询转换为laravel?

$users = DB::table('users a')
            ->join('users b', 'a.id', '=', 'b.head_id')

            ->select('a.head_id as Headid','a.name AS HeadName','b.name AS Emp_name','b.name AS Emp_name')
            ->get();

但运气不好这不起作用。请帮助我。

1 个答案:

答案 0 :(得分:4)

您的查询构建器看起来很好,只需根据您的普通查询添加过滤器

$users = DB::table('users as a')
            ->join('users as b', 'a.id', '=', 'b.head_id')
            ->select('a.head_id as Headid','a.name AS HeadName','b.name AS Emp_name','b.name AS Emp_name')
            ->where('a.head_id', '=', 63)
            ->orWhere('b.id','=', 63)
            ->get();