How to make several separate selects with the 4.2 Standard?

Asked

Viewed 48 times

0

Hello I need to make a select on a table, however if I do not receive the month by the parameter I must do the SUM method to sum all months

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 so above, the Laravel dsconsiders the select within the if

Someone would have a better solution?

  • 1 . What amount is coming in $Month ? 2. From what you say: "receive month by parameter" you could use the isset() function to check this, try a test and see if it works.

  • Hello. What ended up solving my problem was the hiccup described below, but still thank you

  • Laiane has solved his problem accepted as answer to your question!

1 answer

0

You can use the 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, the addSelect adds more elements in the SQL.

Reference: Query Builder - Selects

Browser other questions tagged

You are not signed in. Login or sign up in order to post.