Laravel: Use complex condition in JOIN with Fluent -
for reasons of complex schema & library requires either fluent or eloquent used (not raw db::query() ), need create:
left join `camp_to_cabin` on `camper_to_cabin`.`cabin_id` = `camp_to_cabin`.`cabin_id` , `camp_to_cabin`.`camp_id` =1 or `camp_to_cabin`.`camp_id` null
as join clause; i've tried callbacks & else can think of, cannot proper syntax generate.
i have tried:
->left_join('camp_to_cabin', function ($join){ $join->on( 'camper_to_cabin.cabin_id', '=', 'camp_to_cabin.cabin_id') $join->on( 'camp_to_cabin.camp_id', '=', 1) $join->on( 'camp_to_cabin.camp_id', '=', null) })
but puts backticks around 1 & null (i know null bit isn't right - experimenting) can't rid of; otherwise looks pretty close
any help?
tia
thanks, phil - final answer is:
->left_join('camp_to_cabin', function ($join) use ($id){ $join->on( 'camper_to_cabin.cabin_id', '=', 'camp_to_cabin.cabin_id'); $join->on( 'camper_to_cabin.cabin_id', '=', db::raw($id)); $join->or_on( 'camper_to_cabin.cabin_id', 'is', db::raw('null')); })
looks need use db::raw()
otherwise ->on()
expects 2 columns. like...
->left_join('camp_to_cabin', function ($join){ $join->on( 'camper_to_cabin.cabin_id', '=', 'camp_to_cabin.cabin_id') $join->on( 'camp_to_cabin.camp_id', '=', db::raw(1)) $join->or_on( 'camp_to_cabin.camp_id', '=', db::raw(null)) })
Comments
Post a Comment