How to make several separate selects with laravel 4.2?

0

Hello, I need to do a select in a table, but if I do not receive the month by the parameter I must do the SUM method to sum every month

if($month != 0){
            $dbRegister = tb_data::selectRaw('tb_data.total_points');
        }else{
            $dbRegister = tb_data::selectRaw('sum(tb_data.total_points)');
        }   
$dbRegister->selectRaw('TB_USERS.login,TB_USERS.name)->get()

The problem is that if I do this way above, laravel dsconsidera select within the if

Would anyone have a better solution?

    
asked by anonymous 26.01.2018 / 15:06

1 answer

0

You can use addSelect as follows:

if($month != 0)
{
      $dbRegister = tb_data::selectRaw('tb_data.total_points');
}
else
{
     $dbRegister = tb_data::selectRaw('sum(tb_data.total_points)');
}   


$dbRegister->addSelect('TB_USERS.login','TB_USERS.name')->get();

That is, addSelect adds more elements in SQL .

Query Builder - Selects     
26.01.2018 / 19:24