Laravel Foreach

Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works perfectly with all of Laravel's supported database systems. Google provide powerful programmatic method to get report from Google analytics in your custom dashboard. I am going to integrate Google Analytics API to get summary and api will return following.

October 26, 2018
Laravel Foreach

If you have the following code in a Blade file:

ForeachLaravel foreach key value

You have access to $user (obviously) but also the $loop object.

The $loop object has the following data:

Laravel Foreach In Controller

So if you wanted to do something different for the first or last rows then you could easily do this:

Php Foreach Last Item

PropertyDescription
$loop->indexThe index of the current loop iteration (starts at 0).
$loop->iterationThe current loop iteration (starts at 1).
$loop->remainingThe iterations remaining in the loop.
$loop->countThe total number of items in the array being iterated.
$loop->firstWhether this is the first iteration through the loop.
$loop->lastWhether this is the last iteration through the loop.
$loop->depthThe nesting level of the current loop.
$loop->parentWhen in a nested loop, the parent's loop variable.