Tanvir Ahmed
Tanvir Ahmed

Reputation: 1019

Laravel's dd() only shows first iteration in foreach()

I have sql command like

$kos = DB::select('SELECT team,round,SUM(points) AS total from points WHERE round="first" GROUP by team ORDER BY total desc, run_rate desc limit 4');

When I call dd($kos) it will give me this output see here, but when I run this:

$kos = DB::select('SELECT team,round,SUM(points) AS total FROM points WHERE round="first" GROUP by team ORDER BY total DESC, run_rate DESC LIMIT 4');
foreach($kos as $ko){
        dd($ko->team);
}

it will give me this output see here

Can anyone tell me why?

Upvotes: 1

Views: 457

Answers (1)

Ali Faris
Ali Faris

Reputation: 18592

dd will dump the passed value and exit the execution of the script

in first case you passed a collection to dd , it will dump the whole collection and stop execution of the script

in the second case you're in the first loop and dump the team value and stop execution

if you want just dump the value without stop the execution you should call dump function instead

try this

DB::select('SELECT team,round,SUM(points) AS total from points WHERE round="first" GROUP by team ORDER BY total desc, run_rate desc limit 4');
foreach($kos as $ko){
        dump($ko->team);
} 

Upvotes: 1

Related Questions