更新数据透视表,以备多对多关系laravel4 [英] update pivot table in case of many to many relation laravel4

查看:174
本文介绍了更新数据透视表,以备多对多关系laravel4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始和Laravel4一起工作了。在更新数据透视表数据时,我遇到一些问题,如果有多个关系。



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

  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标签扩展Eloquent {
protected $ table ='tags';
protected $ primaryKey ='tag_id';
public function products()
{
return $ this-> belongsToMany('Product','prd_tags','prta_prd_id','prta_tag_id');
}
}

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

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

但是现在我要更新此数据透视表中的数据,什么是更新数据的最佳方式枢轴表。
我们想删除一些标签,并向特定的产品添加新标签。

解决方案

但是在2013年11月13日,updateExistingPivot方法被公开用于许多关系。这不是官方文档。

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

- 更新表上现有的枢纽记录。 >

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



在您的情况下(如果您想更新您可以执行的枢轴字段'foo'):

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

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

拉请求:



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


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

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

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:

$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.

解决方案

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.

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.

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

$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.

Pull request:

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

这篇关于更新数据透视表,以备多对多关系laravel4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆