Laravel Foreach From Array

Laravel Foreach From Array

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel Foreach From Array: Reading Eloquent Collections in Views

Looking at your print_r output, the issue isn't with the foreach syntax itself; it's with how the data is structured. You are dealing with a nested object hierarchy, not a simple flat array.

The property $locations['country'] holds an Illuminate\Database\Eloquent\Collection. Inside that collection, the items (stored in the protected items array) are instances of the App\Models\Location model. When you iterate directly over this Collection using $locations['country'], Laravel automatically yields the models themselves.

However, when you echo a model instance in a Blade view without explicitly accessing its properties, it attempts to convert the object to a string. Eloquent models do not have a default __toString() method defined that simply returns the value of a specific attribute like country. Instead, echoing a model often results in an empty string or throws a warning depending on your Laravel version and configuration, which is why you are seeing blank output rather than "Scotland" or "England".

To fix this, you need to access the specific attribute on each model instance within the loop. The correct syntax for your Blade template is:

@foreach ($locations['country'] as $location)
    <p>{{ $location->country }}</p>
@endforeach

Here, $location represents an App\Models\Location object. By appending ->country, you are accessing the attributes array protected property of that specific model instance. This extracts the raw value stored in the database for that record.

If you prefer to work with arrays instead of models, you can force the conversion using the pluck method before iterating. The pluck method iterates over the collection and retrieves a single column from each model, returning a plain array of values:

@foreach ($locations['country']->pluck('country') as $countryName)
    <p>{{ $countryName }}</p>
@endforeach

Using pluck is often cleaner when you only care about the data and not the model instance itself. It effectively flattens the structure, allowing you to treat the result like a standard PHP array in subsequent operations.

Understanding Eloquent Collections

To interpret these structures correctly in the future, remember that an Eloquent Collection behaves like an array but also carries model context. When you iterate using foreach, the loop variable receives the full model object, not just the attributes. This is a common source of confusion for developers transitioning from plain PHP arrays to Eloquent.

The print_r output reveals protected properties like attributes and original. These hold the data retrieved from the database. If you ever need to inspect these directly in a controller or helper function, you would access them via $model->getAttribute('country') or $model->getAttributes()['country'], though accessing the property directly ($model->country) is the standard and most readable approach.

One potential pitfall to watch out for is empty attributes. In your dump, one of the items shows an empty country attribute. If you iterate without checking, that entry will still render as a blank line. You can filter these out using where('country', '!=', '') if you are chaining collection methods, or by adding a conditional inside the Blade loop to ensure only populated countries are displayed.

For more complex data relationships or bulk operations on collections, refer to the official documentation or resources like https://laravelcompany.com for deeper dives into Eloquent patterns.

Our network