I have the following two two models in a many-to-many relationship:
use Illuminate\Database\Eloquent\Model; class Permission extends Model { protected $table = 'permissions'; public function roles() { return $this->belongsToMany('App\Admin\Role'); } }
and
class Role extends Model { protected $table = 'roles'; public function users() { return $this->belongsToMany('App\Admin\User'); } public function permissions() { return $this->belongsToMany('App\Admin\Permission'); } }
What I'm trying to do here is to create a page where you can create a new role, and associate this role with the permissions already created:
@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 the selected permissions from the page and save everything:
// logic to save role $role->save(); $permissions = Input::get('permissions'); $role->permissions->sync($permissions);
However, after executing the last statement, I get the following error: exception 'BadMethodCallException' with message 'Method sync does not exist.
"I get the same error for attach
. Also, I'm not sure if I have to specify the name of the permission_role
staging table somewhere? Thanks.
source share