Invalid argument supplied for foreach() in Laravel 5.1

Everything was working fine and I don’t know what happened. I’m getting this error:

Invalid argument supplied for foreach().

I’m trying to get tags that are attached to post like this @foreach($post->tags as $tag) If I do var_dump then it says string(0)"" if I do this with @if(is_array($post->tags)) – no error and no tags. I don’t know where the problem is because the categories are made in the same principle and they are working fine.

This is Post.php model code:

public function tags()
    return $this->belongsToMany('AppTag');

public function getTagListAttribute()
    return $this->tags->lists('id')->all();

This is Tag.php model code:

public function post()
    return $this->belongsToMany('AppPost');

Source: laravel

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.