Laravel 4:多对多(插入)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23908040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:34:00  来源:igfitidea点击:

Laravel 4: many to many (insertion)

phpmysqllaravellaravel-4

提问by mwafi

I have these tables in DB:

我在数据库中有这些表:

[posts, cats (categories), posts_cats (pivote)]

the relation between posts table and cats is many to many

帖子表和猫之间的关系是多对多的

I declared the relation in the models classes:

我在模型类中声明了关系:

//Post.php
public function cats()
{
    return $this->belongsToMany('cats');
}



//Cats.php
public function post()
{
    return $this->belongsToMany('posts');
}

the question is, How to insert new post with multiple categories?

问题是,如何插入具有多个类别的新帖子?

thanks,

谢谢,

回答by Ilyes512

Let's say you know the id of the post then you can attach a single cat like this:

假设您知道帖子的 ID,那么您可以像这样附加一只猫:

Post::find($post_id)->cats()->attach($cat_id);

Or attach multiple cats like this:

或者像这样附加多只猫:

$cat_ids = array(1,2,3,4);
Post::find($post_id)->cats()->attach($cat_ids);

If you got the Post model object in a variable, lets say $post:

如果你在一个变量中得到了 Post 模型对象,让我们说 $post:

$post->cats()->attach($cat_id);

// Or with multiple
$cat_ids = array(1,2,3,4);
$post->cats()->attach($cat_ids);

If you have a single category as an model object in, lets say $model:

如果你有一个单一的类别作为模型对象,让我们说 $model:

$post->cats()->save($model);

Watch out with @Gadoma's answer. Its not wrong, but if you want to add categories to an post that already has categories then you should use attach() instead of sync(). Sync() will delete all others that are not provided to it when used.

注意@Gadoma 的回答。这没有错,但是如果您想为已经有类别的帖子添加类别,那么您应该使用 attach() 而不是 sync()。Sync() 将删除使用时未提供给它的所有其他内容。

edit:
So if you are creating a new Post then you probably are doing something like this:

编辑:
因此,如果您正在创建一个新帖子,那么您可能正在执行以下操作:

$post = new Post;
$post->title = 'The title';
$post->something_else = 'Lorem';
$post->save();

//So now you have both the model object (the $post variable) and the id ($post->id).

$post->cats()->attach($cat_ids);

回答by fmgonzalez

When you insert the post, then itterate over the categories and attach them to the new post. Something like that:

当您插入帖子时,然后遍历类别并将它们附加到新帖子。类似的东西:

// $categories is an array of the categories to attach
foreach ($category_id in $categories) {
    // Get a category object 
    $category = CategoryModel::find($category_id);
    // $post is the new post 
    $post->cats()->attach($category);
}

I hope it helps you.

我希望它能帮助你。

回答by Gadoma

From the docs http://laravel.com/docs/eloquent#inserting-related-models

从文档http://laravel.com/docs/eloquent#inserting-related-models

Inserting Related Models (Many To Many)

[...]

You may also use the sync method to attach related models. The sync method accepts an array of IDs to place on the pivot table. After this operation is complete, only the IDs in the array will be on the intermediate table for the model:

插入相关模型(多对多)

[...]

您也可以使用同步方法来附加相关模型。同步方法接受要放置在数据透视表上的一组 ID。此操作完成后,只有数组中的 ID 将位于模型的中间表中:

And a code example:

和一个代码示例:

$post = new Post(array('field1'=>'value1','fieldN'=>'valueN')) //example create new post
$categoryIds = array(1,3,4,5); //ids of (cats) categories you want the post to go into
$post->cats()->sync($categoryIds); //synchronise pivot table content with $categoryIds