Php 使用同步更新具有Laravel';s关系

Php 使用同步更新具有Laravel';s关系,php,laravel,relationships,Php,Laravel,Relationships,我正在尝试用以下内容更新我的透视表 // Controller if ( Input::has( 'roles' ) ) { $user = $this->user->find( $id ); $user->roles()->sync( Input::get('roles') ); return Redirect::back()->with( 'success', 'You have successfully updated the u

我正在尝试用以下内容更新我的透视表

// Controller

if ( Input::has( 'roles' ) )
{
    $user = $this->user->find( $id );

    $user->roles()->sync( Input::get('roles') );

    return Redirect::back()->with( 'success', 'You have successfully updated the user' );
}


// Model

public function roles()
{
    return $this->belongsToMany('Roles', 'users_roles', 'user_id', 'role_id');
}
当用户更新他们的角色时,它将正常运行,但是只更新或写入1条记录,并且角色id为0

我做错了什么?有人能帮忙吗


谢谢

问题是我没有将整数数组传递给sync方法,所以我将其修改为

$user->roles()->sync( array_keys( Input::get( 'roles' ) ) );

多亏了@alexrussell

大概
Input::get('roles)
返回了一个
数组
,所有数组都是有效的
role\u id
s?如果不是,那可能是你的问题。@Alex Russell有时我看不见树木,它返回的不是一个整数数组,那是我的问题!谢谢你,没问题。你也应该接受你的答案,所以它被标记为正式答案。