2个模型与数据透视表,多对多的关系

时间:2017-07-21 09:58:49

标签: laravel many-to-many pivot

我正在尝试创建一个博客,我有两个模型,Post和Tag。我想用数据透视表连接它们。这是一个多对多的关系,我无法弄清楚如何将帖子和标签链接在一起。当我尝试这样做时,它在我的数据库上没有返回任何内容。帖子有标题和内容,而标签只有名称。 我已经读过我必须使用sync方法或attach-detach但我不知道该怎么做。是在帖子路线或标签路线?我在routes.php中包含了帖子和标签的路由,使用以下方法对它们进行分组:

Route::resource('/tags', 'TagController');

Route::resource('/posts', 'PostController');

这是我到目前为止所拥有的:

我的帖子模型:

class Post extends Model{
protected $fillable = [
    'title', 'content'
];

protected $hidden = [
    'view_count'
];

public function tags()
{
    return $this->belongsToMany('App\Tag', 'post_tag');
}}

这是我的标签模型:

class Tag extends Model{
protected $fillable = [
    'name'
];

public function posts(){
    return $this->belongsToMany('App\Post', 'post_tag');
}}

这是我的post_tag数据透视表:

class CreatePostTagTable extends Migration{

public function up()
{
    Schema::create('post_tag', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('post_id')->unsigned()->nullable()->index();
        $table->foreign('post_id')->references('id')->on('posts');
        $table->integer('tag_id')->unsigned()->nullable()->index();
        $table->foreign('tag_id')->references('id')->on('tags');
        $table->timestamps();
    });
}

public function down()
{
    Schema::dropIfExists('post_tag');
}}

2 个答案:

答案 0 :(得分:1)

嘿,我在laravel也是新人,我和你有同样的问题。在您的情况下,我认为最佳做法是将其附加在您的PostController.php中。让我为您分享代码,我希望它可以提供帮助

<强> PostController中

public function store (Request $request) {

    // First we need to create variable that contain your tag_id
    // $tags = [1, 2, 4]; something like this
    $tags = $request->input('tags');



    // Now storing the data in posts table
    $post =  new Post;
    $post->title = $request->input('title');
    $post->content = $request->input('content');
    $post->save();

    //After save the post, we need to attach it
    $post->tags()->attach($tags);
}

修改:添加示例视图

&#13;
&#13;
<div>
    <input type="checkbox" id="subscribeNews" name="tags[]" value="1">
    <label for="subscribeNews">Tag 1</label>
    <input type="checkbox" id="subscribeNews" name="tags[]" value="2">
    <label for="subscribeNews">Tag 2</label>
    <input type="checkbox" id="subscribeNews" name="tags[]" value="3">
    <label for="subscribeNews">Tag 3</label>
    <input type="checkbox" id="subscribeNews" name="tags[]" value="n">
    <label for="subscribeNews">More tag</label>
  </div>
&#13;
&#13;
&#13;

然后在 PostController.php 中,如果用户选中该复选框,您可以获取ID:

$tags = $request->input('tags');

编辑2:添加示例使用同步

在这里,我举例说明使用同步,首先让我们设置一个有5个标签的帖子。最后我们只想设置它3

$post = Post::find(1) //get the post with id 1, this post have 5 tags
// Let's say that this post have 5 tags with this ids [1, 2, 3, 4, 5]

// And then admin edit this post and select the tag with id [1, 2, 6] we set it in $tags variable
$tags = $request->input('tags'); //[1, 2, 6]

// And the last we just need to use sync method like this
$post->tags()->sync($tags);

// After use that, it will detach the tag based from removed ids [3, 4, 5] and attach new tag if there's new tag id [6]
// And in the end the post just have 3 tags

好的,这是示例逻辑,我仍然可以了解它,但我希望它可以帮助你:D

答案 1 :(得分:1)

使用sync()功能,例如:

$post->tags()->sync($array_of_tags_ids)