将参数绑定传递给 Laravel 查询生成器?

Passing parameter bindings to Laravel query builder?

我目前有一个 SQL 查询,今天早些时候一位出色的 Whosebug 成员帮助我解决了这个问题。目前它工作得非常好,而且是我希望它工作的方式。

唯一的问题是它是 RAW SQL,我非常希望它能与 Laravel 查询生成器一起使用。

抱歉,这是一个编辑 - 这是原始查询:

    $addresses = DB::select(
        DB::raw('
            (SELECT 
                "Company" AS object_type_name,
                companies.company_name AS object_name,
                addresses.*
            FROM 
                addresses
            INNER JOIN 
                companies 
            ON 
                addresses.object_id = companies.id
            WHERE 
                addresses.object_type = 2) 
            UNION ALL 
            (SELECT
                "Job" AS object_type_name,
                jobs.job_title AS object_name,
                addresses.*
            FROM 
                addresses
            INNER JOIN 
                jobs
            ON 
                addresses.object_id = jobs.id
            WHERE 
                addresses.object_type = 4)
        '));

这是我目前的代码:

    $bindings = array(
        'soft_deleted' => 0,
        'user' => 1,
        'company' => 2,
        'candidate' => 3,
        'job' => 4,
    );

    $companies = DB::table('addresses')->select(
      'addresses.*',
      'companies.company_name as object_name'
    )->where('addresses.soft_deleted', '=', 0)->join('companies', function($join) use ($bindings){
        $join->on('addresses.object_id', '=', 'companies.id')
             ->where('addresses.object_type', '=', $bindings['company']);
    });

    $jobs = DB::table('addresses')->select(
      'addresses.*',
      'jobs.job_title as object_name'
    )->join('jobs', function($join) use ($bindings){
        $join->on('addresses.object_id', '=', 'jobs.id')
             ->where('addresses.object_type', '=', $bindings['job']);
    });

    $addresses = $companies->unionAll($jobs)->get();

使用上面的代码我得到以下错误:

SQLSTATE[HY093]: 参数号无效 (SQL: (select addresses., companies.company_name as object_name from addresses inner join companies on addresses.object_id = companies.id and addresses.object_type = 2 其中 addresses.soft_deleted = 0) union all (select addresses., jobs.job_title as object_name from addresses inner join jobs on addresses.object_id = jobs.idaddresses.object_type = ?))

我搜索了好几个小时,但似乎找不到这个小问题的答案。

我设法修复了它。就像从每个连接闭包中删除 ->where 并将其链接到连接本身而不是嵌套它一样简单。

之前:

$companies = DB::table('addresses')->select(
  'addresses.*',
  'companies.company_name as object_name'
)->where('addresses.soft_deleted', '=', 0)->join('companies', function($join) use ($bindings){
    $join->on('addresses.object_id', '=', 'companies.id')
         ->where('addresses.object_type', '=', $bindings['company']);
});

$jobs = DB::table('addresses')->select(
  'addresses.*',
  'jobs.job_title as object_name'
)->join('jobs', function($join) use ($bindings){
    $join->on('addresses.object_id', '=', 'jobs.id')
         ->where('addresses.object_type', '=', $bindings['job']);
});

之后:

    $companies = DB::table('addresses')->select(
      'addresses.*',
      'companies.company_name as object_name'
    )->where('addresses.soft_deleted', '=', 0)->join('companies', function($join){
        $join->on('addresses.object_id', '=', 'companies.id');
    })->where('addresses.object_type', '=', 2);

    $jobs = DB::table('addresses')->select(
      'addresses.*',
      'jobs.job_title as object_name'
    )->where('addresses.soft_deleted', '=', 0)->join('jobs', function($join){
        $join->on('addresses.object_id', '=', 'jobs.id');
    })->where('addresses.object_type', '=', 4);

    $addresses = $companies->unionAll($jobs)->get();

希望这对某人有所帮助。