Here is the thread for reference: Simple tags system in Laravel 5.2
I can't make those tags displayed on the page. It's always return null
here is the source I have:
Item model
public function tags() {
return $this->belongsToMany('App\Tag', 'item_tag');
}
Tag model
class Tag extends Model {
protected $table = 'tags';
protected $primaryKey = 'id';
protected $fillable = [
'tag'
];
public function itemTags() {
return $this->belongsToMany('App\Item', 'item_tag');
}
}
ItemController
public function show($id)
{
$item = Item::with('tags')->find($id);
return view('item', compact('item'));
}
And the view
@foreach($item->tags() as $showTags)
{{ $showTags->tag }}
@endforeach
dd($item)
return two tags in relation so I assume they are there in collection but the return is either empty space on the page or null
.
Item {#322 ▼
#primaryKey: "id"
#table: "items"
#fillable: array:9 [▶]
#connection: null
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:25 [▶]
#original: array:25 [▶]
#relations: array:1 [▼
"tags" => Collection {#332 ▼
#items: array:2 [▼
0 => Tag {#330 ▶} // tag 1
1 => Tag {#331 ▶} // tag 2
]
}
]
...
}
Please suggest what can be the problem.