1

I have a query where i need to use count and have a normal query.

Normal

select sum(total) as Total,AffID,user_id from Affiliates 
group by AffID order by user_id ASC

And Then

foreach($result_array as $row) 
{ 
    echo $row['total']."<br/>"; 
    echo $row['AffID']."<br/>"; 
    echo $row['user_id ']."<br/>"; 
} 

In Laravel i have tried

$affdata = DB::select('Affiliates')
         ->whereRaw('CompletedDate >= curdate()')
         ->groupBy('AffID')
         ->orderBy('user_id', 'ASC')
         ->sum('total');
         ->get();
foreach($affdata as $row) 
{
   echo $row->AffID ."<br>";
   echo $row->total ."<br>";
}

But it seems to throw out an error. As i need to echo out the AFFID along with calculating the Total

2 Answers 2

1

Update your query to this.

$affdata = DB::table('Affiliates')->select(DB::raw('sum(total) as total'), 'AffID', 'user_id'))
            ->whereRaw('CompletedDate >= curdate()')
            ->groupBy('AffID')
            ->orderBy('user_id', 'ASC')
            ->sum('total');
            ->get();
Sign up to request clarification or add additional context in comments.

Comments

0

This is the correct query

$affdata = DB::table('Affiliates')->select(DB::raw('sum(total) as total'), 'AffID', 'user_id')
            ->whereRaw('CompletedDate >= curdate()')
            ->groupBy('AffID')
            ->orderBy('user_id', 'ASC')
            ->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.