は、参照のためのスレッドです:Simple tags system in Laravel 5.2データはlaravel関係に+ 1ピボットテーブル2つのテーブルから来ていない
私がページに表示されるこれらのタグを作成することはできません。それはいつもここnull
を返しています私が持っているソースです:
項目モデル
public function tags() {
return $this->belongsToMany('App\Tag', 'item_tag');
}
Tagモデル
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'));
}
とビュー
@foreach($item->tags() as $showTags)
{{ $showTags->tag }}
@endforeach
dd($item)
2つのタグが関連しているので、それらはコレクションにあると仮定しますが、返されるのはページ上の空白か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
]
}
]
...
}
問題が発生する可能性があります。あなたが関係、コレクションとしての利用可能を起動する必要はありません熱望してロードした後
あなたTagモデルは、 'tag'という名前のプロパティを持っていますか? – Jerodev
@Jerodev完全な 'Tag'モデル – VLS