Laravel,函数创建和编辑,从标签中检索数据

我有一个基本的文章和标签系统,具有多对多关系。我设法在创建功能中显示所有标签,但我不知道如何显示所有标签和在编辑功能中选中的标签。


文章 :


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>

真的需要帮助,这是一个学校项目。对不起我的英语......创建: 添加文章的屏幕 编辑: 编辑文章的屏幕


千巷猫影
浏览 84回答 1
1回答

繁星点点滴滴

我在这里找到了!只需要一秒钟foreach@foreach ($tags as $tag)&nbsp; &nbsp;<label><input type="checkbox" name="tags[]" value="{{ $tag->id }}"&nbsp; &nbsp; &nbsp; @foreach ($article->tags as $article_tags)&nbsp; &nbsp; &nbsp; &nbsp; {{ $article_tags->id == $tag->id ? 'checked' : '' }}&nbsp; &nbsp; &nbsp; @endforeach>{{ $tag->name }}</label>@endforeach并在创建$tags= Tag::all();无论如何感谢您的帮助^^
打开App,查看更多内容
随时随地看视频慕课网APP