mysql - How to combine dynamic fields and DB::raw with Laravel's fluent query builder? -
this desired sql:
select p.id , p.title , p.msrp , i.files , p.cost `products` p left join ( select group_concat( `images`.`filename` separator ',' ) files , `product_id` `images` group `product_id` ) on i.product_id = p.id here query using laravel's fluent query builder:
$products = db::table( 'products p' ) ->where( 'p.active' , '=' , 1 ) ->select( $fields ) ->get(); the $fields variable array of fields based on whether user logged in or not:
$fields = array( 'p.id' , 'p.title' , 'p.msrp' , 'i.files' ) ; if( auth::check() ) array_push( $fields , 'p.cost' ) ; i trying left join using db::raw:
->leftjoin(db::raw( "( select group_concat( images.filename separator ',' ) , product_id images group product_id ) i" , "i.product_id", "=" , "p.id" ) for reason not working... doing wrong? need use db::raw can group_concat multiple rows in images table delimited string.
on tangent... think sub-query select not supported if has better option, please let me know.
Comments
Post a Comment