我在尝试获取视图中的数据时收到错误。这是我的代码:
的控制器
public function create()
{
$categories = Category::all();
$tag = Tag::groupBy('name')->pluck('name');
$tags = json_encode($tag);
return view('backend.articles.create', compact('categories', 'tags'));
}
查看
获取数据类别
<div class="panel-body">
@foreach ($categories as $category)
<div class="checkbox">
<label>
{!! Form::checkbox('category_id[]', $category->id) !!} {{ $category->name }}
</label>
</div>
@endforeach
</div>
获取数据标记
jQuery(document).ready(function($) {
$('#tags').magicSuggest({
cls: 'form-control',
data: {!!$tags!!},
@if (count(old('tags')))
value: [
@foreach (old('tags') as $tag)
'{{$tag}}',
@endforeach
]
@endif
});
});
我不知道为什么会收到此错误消息:为foreach()提供的参数无效。有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
视图
<div class="panel-body">
@if($categories)
@foreach ($categories as $category)
<div class="checkbox">
<label>
{!! Form::checkbox('category_id[]', $category->id) !!} {{ $category->name }}
</label>
</div>
@endforeach
@endif
脚本
jQuery(document).ready(function($) {
$('#tags').magicSuggest({
cls: 'form-control',
data: {!!$tags!!},
@if (is_array(old('tags')))
value: [
@foreach (old('tags') as $tag)
'{{$tag}}',
@endforeach
]
@endif
});
});
尝试使用is_array来确保旧的('tags')返回数组,我相信这个问题是因为旧的('tags')没有返回数组。