Please follow the example below to write the the Multiple Join Query on Laravel 5.2
Output Query (will produce the following JOIN query):
Hope it helps!!!
// NOTE: Please do not forgot add below line at the top to use DB instance USE DB;
$whereClause = array( $this->table.".checklists_id" => 1 ); $items = DB::table($this->table) ->join('approval_states', 'approval_states.id', '=', $this->table.'.approval_state_id') ->join('approval_levels', 'approval_levels.id', '=', 'approval_level_conditions.approval_levels_id') ->select( $this->table.'.*', 'approval_levels.name as level_name', 'approval_levels.level', 'approval_states.state', DB::raw("IF( approval_states.state='Approved','1','0' ) AS state_value") ) ->where($whereClause) ->where($this->table.'.deleted_at', null) // to ignore the soft deleted records. ->get();
Output Query (will produce the following JOIN query):
SELECT `approval_checklists`.*, `approval_levels`.`name` AS `level_name`, `approval_levels`.`level`, `approval_states`.`state`, IF( approval_states.state='Approved','1','0' ) AS state_value FROM `approval_checklists` INNER JOIN `approval_states` ON `approval_states`.`id` = `approval_checklists`.`approval_state_id` INNER JOIN `approval_levels` ON `approval_levels`.`id` = `approval_level_conditions`.`approval_levels_id` WHERE (`approval_levels`.`checklists_id` = 1) AND `approval_checklists`.`deleted_at` IS NULL
Hope it helps!!!
No comments:
Post a Comment
Please post any queries and comments here.