简体   繁体   English

Laravel 5插入带有属性的数据透视表

[英]Laravel 5 insert pivot table with attributes

I've been learning laravel for a few days now and I was hoping you guy's could help me. 我已经学习了几天laravel,我希望你的家伙可以帮助我。 I have the following table structure: 我有以下表结构:

users
------
id


companies
------
id


company_user
------
company_id
user_id
owner

My User and Company models have functions specified like this: 我的用户和公司模型具有如下指定的功能:

User Model 用户模型

public function companies() {
    return $this->belongsToMany('App\Company', 'company_user', 'company_id', 'user_id')->withPivot('owner');
}

Company Model 公司模式

public function users() {
    return $this->belongsToMany('App\User', 'company_user', 'company_id', 'user_id')->withPivot('owner');
}

For example when I save a company to a user like this: 例如,当我将公司保存给这样的用户时:

User::find(1)->companies()->save(\App\Company::find(1));

The pivot table gets filled nicely, but the owner column doesn't get filled. 数据透视表很好地填充,但所有者列不会被填充。 I can't find many online how to fill this column. 我在网上找不到很多如何填写这个专栏。 Hope you guys can help me? 希望你们能帮助我吗?

Thanks in advance! 提前致谢!

您可以将其他数据作为第二个参数传递给save()

User::find(1)->companies()->save(\App\Company::find(1), ['owner' => 'foo']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM