Laravel 5.3:使用foreach在视图中获取数据时出错


i got error while i'm trying to fetch my data in view. This is my code :

controller

public function create()
{
    $categories = Category::all();
    $tag = Tag::groupBy('name')->pluck('name');
    $tags = json_encode($tag);

    return view('backend.articles.create', compact('categories', 'tags'));
}

view
fetch data categories

<div class="panel-body">
    @foreach ($categories as $category)
        <div class="checkbox">
           <label>
                  {!! Form::checkbox('category_id[]', $category->id) !!} {{ $category->name }}
           </label>
        </div>
    @endforeach
 </div>

fetch data tags

jQuery(document).ready(function($) {
        $('#tags').magicSuggest({
            cls: 'form-control',
            data: {!!$tags!!},
            @if (count(old('tags')))
            value: [
                @foreach (old('tags') as $tag)
                        '{{$tag}}',
                @endforeach
            ]
            @endif
        });
    });

I dont know why i got this error message : Invalid argument supplied for foreach(). Could anybody to help me to fix this problem ?

view

<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

script

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
    });
});

Try to use is_array to make sure old('tags') returning array, I believe the problem because the old('tags') is not returning array.