php – 如何在laravel刀片视图上定义变量数组?
作者:互联网
我试着这样:
<div class="media-body">
@foreach($categories as $category)
@php $category[] = $category->name @endphp
@endforeach
{{ implode(",", $category) }}
</div>
如果代码执行,则存在错误:
undefine variable category
我该如何解决?
解决方法:
您只需使用Laravel Collection即可
{{ $categories->pluck('name')->implode(', ') }}
或者,如果你想在foreach中这样做
@php ($names = [])
@foreach ($categories as $category)
@php ($names[] = $category->name)
@endforeach
{{ implode(', ', $names) }}
标签:laravel-5-3,laravel-blade,implode,php,laravel 来源: https://codeday.me/bug/20190828/1751820.html