php - Passing parameter bindings to Laravel query builder? -
i have sql query fantastic stackoverflow member helped me earlier today. @ moment works absolutely fine , way want work.
the problem it's raw sql , i'd working laravel query builder.
sorry edit - here original query:
$addresses = db::select( db::raw(' (select "company" object_type_name, companies.company_name object_name, addresses.* addresses inner join companies on addresses.object_id = companies.id addresses.object_type = 2) union (select "job" object_type_name, jobs.job_title object_name, addresses.* addresses inner join jobs on addresses.object_id = jobs.id addresses.object_type = 4) '));
here code have far:
$bindings = array( 'soft_deleted' => 0, 'user' => 1, 'company' => 2, 'candidate' => 3, 'job' => 4, ); $companies = db::table('addresses')->select( 'addresses.*', 'companies.company_name 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 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();
with code above i'm getting following error:
sqlstate[hy093]: invalid parameter number (sql: (select addresses
., companies
.company_name
object_name
addresses
inner join companies
on addresses
.object_id
= companies
.id
, addresses
.object_type
= 2 addresses
.soft_deleted
= 0) union (select addresses
., jobs
.job_title
object_name
addresses
inner join jobs
on addresses
.object_id
= jobs
.id
, addresses
.object_type
= ?))
i've done hours worth of searching can't seem find answer little problem.
i managed fix it. simple removing ->where
within each join closure , chaining join , not nesting it.
before:
$companies = db::table('addresses')->select( 'addresses.*', 'companies.company_name 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 object_name' )->join('jobs', function($join) use ($bindings){ $join->on('addresses.object_id', '=', 'jobs.id') ->where('addresses.object_type', '=', $bindings['job']); });
after:
$companies = db::table('addresses')->select( 'addresses.*', 'companies.company_name 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 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();
hope helps someone.
Comments
Post a Comment