Laravel Foreach From Array
What you're looping over isn't an array. It's a Laravel Collection. However it behaves like an array so it doesn't really matter. The loop itself actually looks correct. But instead of just outputting $country
you have to actually access the attribute on $country
called country
:
@foreach($locations['country'] as $location)
{{ $location->country }}
@endforeach
In general a foreach
loop goes over every item in an array or collection and put's that item into the variable you define after as
. Maybe this explanation helps too.
As a little extra: Laravel has a nice lists()
function which builds an array out of an attribute from every model in a collection.
$countries = $locations['country']->lists('country');
Would result in something like:
['England', 'Wales', 'Scotland']
And you could then use functions like implode()
to generate a comma separated list:
implode(', ', $countries); // returns 'England, Wales, Scotland'
mikelovelyuk
Updated on July 09, 2022Comments
-
mikelovelyuk almost 2 years
Here is the
print_r
of my object;Array ( [country] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\Models\Location Object ( [connection:protected] => [table:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [country] => Scotland ) [original:protected] => Array ( [country] => Scotland ) [relations:protected] => Array ( ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [appends:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [casts:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 ) [1] => App\Models\Location Object ( [connection:protected] => [table:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [country] => England ) [original:protected] => Array ( [country] => England ) [relations:protected] => Array ( ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [appends:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [casts:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 ) [2] => App\Models\Location Object ( [connection:protected] => [table:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [country] => Wales ) [original:protected] => Array ( [country] => Wales ) [relations:protected] => Array ( ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [appends:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [casts:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 ) [3] => App\Models\Location Object ( [connection:protected] => [table:protected] => [primaryKey:protected] => id [perPage:protected] => 15 [incrementing] => 1 [timestamps] => 1 [attributes:protected] => Array ( [country] => ) [original:protected] => Array ( [country] => ) [relations:protected] => Array ( ) [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [appends:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) [dates:protected] => Array ( ) [casts:protected] => Array ( ) [touches:protected] => Array ( ) [observables:protected] => Array ( ) [with:protected] => Array ( ) [morphClass:protected] => [exists] => 1 ) ) ) )
I want a foreach loop to print out the three countries in the
country
array - (England, Wales, Scotland).I have tried loops such as;
@foreach ($locations['country'] as $country) {{ $country }} @endforeach
I have tried other variations of this but to no avail. What is the correct syntax? Also, can someone explain how I can interpret this so I can better understand foreach with arrays in future? I normally just end up guessing until I get the right result - but for a change I'd like to actually know how to put one together if that makes sense..
I am using Laravel if it helps...