我有一個基本的文章和標簽系統,具有多對多關系。我設法在創建功能中顯示所有標簽,但我不知道如何顯示所有標簽和在編輯功能中選中的標簽。文章 :public function tags(){ return $this->belongsToMany(Tag::class)->withTimestamps();}標簽 :public function article(){ return $this->belongsToMany(Article::class)->withTimestamps();}函數創建:public function create(){ $article = new Article(); $tags = $article->tags = Tag::all(); return view('articles.create', compact('article','tags'));}功能編輯public function edit(Article $article){ return view('articles.edit', compact('article'));}形式<div class="form-group"> @foreach($article->tags as $tag) <label><input type="checkbox" name="tags[]" value="{{ $tag->id }}" /> {{ $tag->name }}</label> @endforeach</div>真的需要幫助,這是一個學校項目。對不起我的英語......創建: 添加文章的屏幕 編輯: 編輯文章的屏幕
1 回答

繁星點點滴滴
TA貢獻1803條經驗 獲得超3個贊
我在這里找到了!只需要一秒鐘foreach
@foreach ($tags as $tag)
<label><input type="checkbox" name="tags[]" value="{{ $tag->id }}"
@foreach ($article->tags as $article_tags)
{{ $article_tags->id == $tag->id ? 'checked' : '' }}
@endforeach>{{ $tag->name }}</label>
@endforeach
并在創建$tags= Tag::all();
無論如何感謝您的幫助^^
- 1 回答
- 0 關注
- 123 瀏覽
添加回答
舉報
0/150
提交
取消