Hi everybody!
I'm a newbie in laravel and I'm trying to create a interface to manage users roles and permissions created by entrust package. I have already defineded my relation belongs to many in both permission and role models. How can i get all the permissions from one role and also the others that aren't associated with the role with eloquent. How can I do this?
I want something like this to show all permissions and show the ones that are selected in the view.
|---------------------|------------|
|Permission_id |Role_id |
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | NULL |
I did this
edit.blade.php
{{ Form::model($role->first(), ['role' => 'form', 'url' => '/admin/roles/' . $role->first()->id, 'method' => 'PUT']) }}
<div class='form-group'>
{{ Form::label('name', 'Nome') }}
{{ Form::text('name', null, ['placeholder' => 'Nome', 'class' => 'form-control']) }}
</div>
@foreach ($perms as $perm)
<div class='form-group'>
{{Form::label('perm_name'.$perm->id, $perm->name );}}
{{Form::label('perm_name_d'.$perm->id, $perm->display_name );}}
{{Form::checkbox( $perm->id, $perm->id, $permRole->contains($perm->id) ? 1 : 0); }}
</div>
@endforeach
<div class='form-group'>
{{ Form::submit('Actualizar', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
Controller.php
edit function
$perms = Permission::all();
$role = Role::whereId($id)->get();
$permRole = $role->first()->permissions()->get();
return View::make('admin.roles.edit', [ 'role' => $role, 'perms' => $perms, 'permRole' => $permRole ]);
update function
$permissions = Permission::all();
$role = Role::find($id);
$role->name = Input::get('name');
$role->save();
$role->permissions()->detach();
foreach ($permissions as $key) {
if (Input::has($key->id)) {
$role->permissions()->attach($key->id);
}
}
return Redirect::to('/admin/roles');
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community