laravel 保存多对多关系,同步/附加不存在?

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

Saving many-to-many relationship, sync/attach doesn't exist?

phplaravellumen

提问by Zed

I have two following 2 models in many-to-many relationship :

我有以下两个多对多关系的模型:

use Illuminate\Database\Eloquent\Model;

class Permission extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'permissions';

    /*
    |--------------------------------------------------------------------------
    | Relationship Methods
    |--------------------------------------------------------------------------
    */

    /**
     * many-to-many relationship method
     *
     * @return QueryBuilder
     */
    public function roles()
    {
        return $this->belongsToMany('App\Admin\Role');
    }

}

and

class Role extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'roles';

    /*
    |--------------------------------------------------------------------------
    | Relationship Methods
    |--------------------------------------------------------------------------
    */

    /**
     * many-to-many relationship method.
     *
     * @return QueryBuilder
     */
    public function users()
    {
        return $this->belongsToMany('App\Admin\User');
    }

    /**
     * many-to-many relationship method.
     *
     * @return QueryBuilder
     */
    public function permissions()
    {
        return $this->belongsToMany('App\Admin\Permission');
    }
}

What I'm trying to do here, is to create a page where new Role can be created, and associate that role with already created Permissions:

我在这里尝试做的是创建一个可以创建新角色的页面,并将该角色与已创建的权限相关联:

@foreach ($permissions as $permission)
                            <label class="checkbox">
                                <input type="checkbox" value="{{ $permission->id }}" name="permissions[]" id="permission_{{ $permission }} }}">
                                {{ $permission->permission_title }}
                            </label>
                        @endforeach

and in the controller I tried this to extract selected permissions from the page and save everything:

在控制器中,我尝试从页面中提取选定的权限并保存所有内容:

// logic to save role
$role->save();
$permissions = Input::get('permissions');
$role->permissions->sync($permissions);

However after the last statement is executed I get the following error: exception 'BadMethodCallException' with message 'Method sync does not exist.' The same error I get for attachas well. Also, I'm not sure if I'm supposed to provide somewhere the name of the intermediate table permission_role? Thanks.

但是,在执行最后一条语句后,我收到以下错误: exception 'BadMethodCallException' with message 'Method sync does not exist.' 我也遇到了同样的错误attach。另外,我不确定是否应该在某处提供中间表的名称permission_role?谢谢。

回答by Hammerbot

You need to use the following:

您需要使用以下内容:

$role->permissions()->sync($permissions);

Don't forget the ()

不要忘记 ()

EDIT: Some more explanations:

编辑:更多解释:

$role->permissionsis a Collection instance.

$role->permissions是一个集合实例。

$role->permissions()is a belongsToManyinstance which contains the sync()method

$role->permissions()是一个belongsToMany包含sync()方法的实例