laravel 在多对多关系 laravel4 的情况下更新数据透视表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15621279/
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 07:38:43  来源:igfitidea点击:

update pivot table in case of many to many relation laravel4

many-to-manylaravelpivot-tablelaravel-4eloquent

提问by Sameer

I have started working with Laravel4 recently. I am facing some problem while updating pivot table data, in case of many to many relation.

我最近开始使用 Laravel4。在多对多关系的情况下,我在更新数据透视表数据时遇到了一些问题。

The situation is: I have two table: Product, ProductType. The relation between them is Many to many. My Models are

情况是:我有两个表:ProductProductType。它们之间的关系是多对多的。我的模型是

class Product extends Eloquent {
    protected $table = 'products';
    protected $primaryKey = 'prd_id';

    public function tags() {
        return $this->belongsToMany('Tag', 'prd_tags', 'prta_prd_id', 'prta_tag_id');
    }
}

class Tag extends Eloquent {
    protected $table = 'tags';
    protected $primaryKey = 'tag_id';
        public function products()
    {
    return $this->belongsToMany('Product', 'prd_tags', 'prta_prd_id', 'prta_tag_id');
    }
}

While inserting data to the pivot table prd_tags, I did:

在向数据透视表 prd_tags 插入数据时,我做了:

$product->tags()->attach($tag->tagID);

But now I want to update data in this pivot table, what is the best way to update data to the pivot table. Let's say, I want to delete some tags and add new tags to a particular product.

但是现在我想更新这个数据透视表中的数据,将数据更新到数据透视表的最佳方法是什么。比方说,我想删除一些标签并向特定产品添加新标签。

回答by Andrew

Old question, but on Nov 13, 2013, the updateExistingPivot method was made public for many to many relationships. This isn't in the official documentation yet.

老问题,但在 2013 年 11 月 13 日,updateExistingPivot 方法为多对多关系公开。这还没有在官方文档中。

public void updateExistingPivot(mixed $id, array $attributes, bool $touch)

--Updates an existing pivot record on the table.

-- 更新表上现有的数据透视记录。

As of Feb 21, 2014 you must include all three arguments.

自 2014 年 2 月 21 日起,您必须包含所有三个参数。

In your case, (if you wanted to update the pivot field 'foo') you could do:

在您的情况下,(如果您想更新数据透视字段“foo”),您可以执行以下操作:

$product->tags()->updateExistingPivot($tag->tagID, array('foo' => 'value'), false);

Or you can change the last boolean false to true if you want to touch the parent timestamp.

或者,如果您想触摸父时间戳,您可以将最后一个布尔值 false 更改为 true。

Pull request:

拉取请求:

https://github.com/laravel/framework/pull/2711/files

https://github.com/laravel/framework/pull/2711/files

回答by Gokigooooks

Another method for this while working with laravel 5.0+

使用 laravel 5.0+ 时的另一种方法

$tag = $product->tags()->find($tag_id);
$tag->pivot->foo = "some value";
$tag->pivot->save();

回答by Shishir

I know that this is an old question, but if you're still interested in the solution, here it is:

我知道这是一个老问题,但如果您仍然对解决方案感兴趣,请看这里:

Lets say your pivot table has 'foo' and 'bar' as the additional attributes, you can do this to insert data into that table:

假设您的数据透视表具有 'foo' 和 'bar' 作为附加属性,您可以这样做以将数据插入该表:

$product->tags()->attach($tag->tagID, array('foo' => 'some_value', 'bar'=>'some_other_value'));

回答by Hamid Naghipour

this is full example :

这是完整的例子:

 $user = $this->model->find($userId);
    $user->discounts()
        ->wherePivot('discount_id', $discountId)
        ->wherePivot('used_for_type', null)
        ->updateExistingPivot($discountId, [
            'used_for_id' => $usedForId,
            'used_for_type' => $usedForType,
            'used_date_time' => Carbon::now()->toDateString(),
        ], false);