phplaravellaravel-5.8dumpdd

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?


Solution

  • 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);
    }