dongtao9095 2016-03-06 12:40
浏览 91
已采纳

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

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 attach as well. Also, I'm not sure if I'm supposed to provide somewhere the name of the intermediate table permission_role ? Thanks.

  • 写回答

1条回答 默认 最新

  • duanpendan8067 2016-03-06 12:45
    关注

    You need to use the following:

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

    Don't forget the ()

    EDIT: Some more explanations:

    $role->permissions is a Collection instance.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。