Laravel Foreach 来自数组

Laravel Foreach From Array

这是我对象的print_r

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
                    )

            )

    )
)

我想要一个 foreach 循环来打印出 country 数组中的三个国家 -(英格兰、威尔士、苏格兰)。

我试过循环,例如;

@foreach ($locations['country'] as $country)
 {{ $country }}
@endforeach

我已经尝试过此的其他变体,但无济于事。正确的语法是什么?另外,有人可以解释我如何解释这个,以便我以后可以更好地理解数组的 foreach 吗?我通常只是猜测,直到我得到正确的结果 - 但对于一个改变,我想真正知道如何将它们组合在一起,如果这有意义的话..

如果有帮助,我正在使用 Laravel...

您要循环的不是数组。这是一个 Laravel 合集。但是它的行为就像一个数组,所以它并不重要。循环本身实际上看起来是正确的。但是,您不仅要输出 $country,还必须实际访问 $country 上名为 country:

的属性
@foreach($locations['country'] as $location)
    {{ $location->country }}
@endforeach

通常,foreach 循环遍历数组或集合中的每个项目,并将该项目放入您在 as 之后定义的变量中。也许 this explanation 也有帮助。


作为额外的一点:Laravel 有一个很好的 lists() 函数,它根据集合中每个模型的属性构建一个数组。

$countries = $locations['country']->lists('country');

会导致类似的结果:

['England', 'Wales', 'Scotland']

然后您可以使用 implode() 之类的函数生成逗号分隔列表:

implode(', ', $countries); // returns 'England, Wales, Scotland'