Support the ongoing development of Laravel.io →
Database Eloquent Views

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   |

Last updated 2 years ago.
0

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');
Last updated 2 years ago.
0

You can also use the sync() function

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.