laravel entrust how to check a role's permission in blade? - laravel

I am using entrust in my laravel project.
In blade file when I edit a role I want to display all the permissions with checkbox.
But I stuck at the thought that I want the checkbox status is checked if the role has the permission.I pass the role and all permissions to blade ,and try
#foreach($permissions as $permission)
<input type="checkbox" value="{{$permission->name}}"
#if($role->hasPermission($permission->name))
checked="checked"
#endif
#endforeach
but it didn't work
I also try convert $role and $permissions to array and pass them to blade and use foreach twice ,it didn't work either.
Is there any way to make it?

You can try this:
#foreach($permissions as $permission)
#foreach($roles as $role)
#if($permission->hasRole($role->name))
<input type="checkbox" checked="checked" name="perms[[]" value="{{ $permission->id }}">
#else
<input type="checkbox" name="perms[]" value="{{ $permission->id }}">
#endif
#endforeach
#endforeach

turns out that $role also can call the hasPermission method
#foreach($permissions as $permission)
<div class="checkbox pull-left" >
<label class="">
<input type="checkbox" name="perms[]" value="{{$permission->id}}"
#if($role->hasPermission($permission->name)) checked #endif>
<p>{{$permission->display_name}}</p>
</label>
</div>
#endforeach

Related

create dynamically checkbox in blade

I´m traying to create list of checkbox in my blade with all roles that i have in my DB. I´m doing loop to get all roles and if to check that roles have this user that i´m doing edit:
#foreach($roles as $rol)
#foreach ($selRoles as $role)
#if ($role == $rol->id)
<div class="col-md-4">
<input type="checkbox" name="rol" checked=checked class="form-check-input" value="{{ $rol->id }}" id="{{ $rol->id }}">
{{ $rol->name }}
</div>
#else
<div class="col-md-4">
<input type="checkbox" name="rol" class="form-check-input" value="{{ $rol->id }}" id="{{ $rol->id }}">
{{ $rol->name }}
</div>
#endif
#endforeach
#endforeach
With this code, my problem it´s that all my roles it´s duplicated. In my controller i have this:
public function edit(User $usuario)
{
$roles = Bouncer::role()->orderBy('title', 'DESC')->get();
$selRoles = $usuario->roles->pluck('id')->toArray(); //selRoles it´s roles from user
$usuario->load('roles');
return view('admin.empleados.edit', compact('usuario', 'roles','selRoles'));
}
and retult in blade it´s:
teleoperadora
teleoperadora
teleoperadora
repartidor
repartidor
repartidor
prueba
prueba
prueba
Jefe de equipo
Jefe de equipo
Jefe de equipo
jefa-sala
jefa-sala
jefa-sala
instalador
instalador
for example. I don´t know that i´m doing bad for get this result in my blade. Anybody can help me please?
Thanks for read and help. Sorry for my english
Your roles get duplicated because you are using two foreach loops. Your selected roles are an array, so it should suffice to check if a role->id is in that array:
#foreach($roles as $role)
<div class="col-md-4">
<input type="checkbox" name="rol" #if(in_array($role->id, $selRoles))checked=checked#endif class="form-check-input" value="{{ $role->id }}" id="{{ $role->id }}">
{{ $role->name }}
</div>
#endforeach
The part #if(in_array($role->id, $selRoles))checked=checked#endif will check the checkbox if the role's id is in your array of selected roles.

How to fill the form checkboxes with checked data from database laravel 5.8

I'm having a problem in my update Form,
I create user with multiple roles assigned using checkboxes in my form, and when I need to update a user i get all the data into my update form input fields except the checkbox fields will be empty. Is there a way that I can get the checkbox checked with the data from database ?
Here's my User Model Relationship
public function roles()
{
return $this->belongsToMany(Role::class)->withTimestamps();
}
Here's my Role Model Relationship
public function users()
{
return $this->belongsToMany(User::class)->withTimestamps();
}
Here's my Edit User Form
<form action="{{ route('users.update',['user'=>$user]) }}" method="POST">
#method('PATCH')
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" value="{{ old('name') ?? $user->name }}">
<span class="text-danger">{{ $errors->first('name') }}</span>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" name="email" class="form-control" value="{{ old('email') ?? $user->email }}">
<span class="text-danger">{{ $errors->first('email') }}</span>
</div>
<div class="form-group">
#foreach ($roles as $role)
<label for="role">{{ $role->name }}</label>
<input type="checkbox" name="role[{{$role->id}}]" value="{{ old($role->id) }}"
#if(in_array($role->id,old('role',[]) )) checked #endif >
#endforeach
<span class="text-danger">{{ $errors->first('role') }}</span>
</div>
#csrf
<button class="btn btn-primary" type="submit">Create</button>
</form>
Here's my Edit Function
public function edit(User $user)
{
$roles = Role::all();
return view('users.edit', compact('user', 'roles'));
}
I just wanted to know how can I pull data into my checkboxes from the database and mark them as checked
I guess you should check role id with the user role id:
#if(in_array($role->id, $user->roles->toArray())) checked #endif
Finally I found out the answer
<div class="form-group">
#foreach ($roles as $role)
<label for="role">{{ $role->name }}</label>
<input type="checkbox" name="role[]" value="{{ $role->id }}"
{{ $role->users->contains($user->id) ? 'checked' : '' }}
#if(in_array($role->id,old('role',[]))) checked #endif>
#endforeach
<span class="text-danger">{{ $errors->first('role') }}</span>
</div>

Mark Dynamically Created Checkbox Checked using Laravel

I'm trying to develop roles and permissions in laravel. I can now make the roles work properly, however i just have an issue of marking the checkbox checked of the current permissions of the user.
This is my current controller code
$role = Role::findOrFail($id);
$perm = Role::findOrFail($id)->permissions;
$permissions = Permission::all();
return view('Roles.edit', compact('permissions', 'role', 'perm'));
and for my view
#foreach($permissions as $perms)
<div class="col-lg-12">
<div class="input-group m-input-group m-input-group--square">
<input type="checkbox" name="permission[]" value="{{ $perms->id }}" />
<label>{{ ucwords(str_replace('-', ' ', $perms->name)) }}</label>
</div>
</div>
#endforeach
First of all your variable names are really bad, so try to improve those so later you know what is going on :)
I would change this:
$perm = Role::findOrFail($id)->permissions;
to this one:
$rolePermissions = Role::findOrFail($id)->permissions;
then in your view, you will need to check if the current permission is contained in the $rolePermissions and add the checked attribute to the checkbox:
#foreach($permissions as $permission)
<div class="col-lg-12">
<div class="input-group m-input-group m-input-group--square">
<input type="checkbox" name="permission[]"
value="{{ $permission->id }}"
#if(in_array($permission->id, $rolePermissions)) checked #endif />
<label>{{ ucwords(str_replace('-', ' ', $permission->name)) }}</label>
</div>
</div>
#endforeach
I guess your $rolePermissions is a set of ids. If it is not just get the ids from the collection:
$rolePermissions = Role::findOrFail($id)->permissions->pluck('id');

Laravel post radio button

I want to send the value of the radio button to the class, the problem is that the name is different each time because I have an id to finally group it in the view, how can I refer to this name?
#foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>
<form method="post" action="updateprivileges/{{$user->id}}">
#csrf
<div class="row">
<div class="col-4">
<input type="radio" value="1" class="flat" name="privileges_{{ $user->id }}" {{ $user->privileges[0]['roles_id'] == '1' ? 'checked' : '' }} > <div class="uprawnienia-margin">Administrator</div>
</div>
<div class="col-4">
<input type="radio" value="2" class="flat" name="privileges_{{ $user->id }}" {{ $user->privileges[0]['roles_id'] == '2' ? 'checked' : '' }} > <div class="uprawnienia-margin">Serwisant</div>
</div>
<div class="col-4">
<input type="radio" value="3" class="flat" name="privileges_{{ $user->id }}" {{ $user->privileges[0]['roles_id'] == '3' ? 'checked' : '' }} > <div class="uprawnienia-margin">Monter</div>
</div>
</div>
<input type="submit" name="add" class="btn btn-primary input-lg" value="Przeslij" />
</form>
</td>
</tr>
#endforeach
You have dynamic name attributes. You should change this to static ones, so you can always receive the correct value in your controller where you receive the payload. And don't forget to use the correct HTTP Method.
<form method="post" action="{{ URL::to('/updateprivileges/' . $user->id)}}">
#csrf
<input type="radio" name="privilege" value="1"> Administrator</br>
<input type="radio" name="privilege" value="2"> Serwisant</br>
<input type="radio" name="privilege" value="3"> Monter</br>
<input type="submit" value="Przeslij">
</form>
Declare the route in routes/web.php:
// put method
Route::post('updateprivileges/{id}', 'UserController#updatePrivileges');
Now, your UserController receives the privilege variable in the Request $request:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function updatePrivileges(Request $request, $id)
{
$privilege = $request->input('privilege');
dd($privilege); // dumps $privilege and dies
// more code, perhaps save the users privilege
}
}
I hope this helps.
You might want to check out Laravel Method Spoofing and Put, Post, Patch. Since you know the user (by id), you might want to switch to the PUT or PATCH method.

Laravel - Highlighting roles which are already assigned to a user

On user\edit view I'm displaying all roles as checkboxes. I'd like to have roles which are already assigned to a user checked. All other fields in the form are filled in with details from the database already. I'm trying to work with Spatie's Laravel Permissions.
Image of the view
#foreach ($roles as $role)
<input type="checkbox" value="{{$role->id}}" name="{{$role->name}}" > {{$role->name}}</input>
#endforeach
I would like to use pure HTML. I read about eager loading but from what I understood it will give me only roles assigned to a user, not all of them.
I want to use sychRoles() function when I click update button.
I was hoping to build similar view to what Voyager uses: example.
try below:
in controller:
$roles = $user->roles;
in blade file:
#foreach ($roles as $role)
<input type="checkbox" value="{{$role->id}}" name="{{$role->name}}"> {{$role->name}}</input>
#endforeach
Try this. It will check if there is any logged in user. Then it will match the roles of that user with id from roles relationship.
#foreach ($roles as $role)
<input type="checkbox" value="{{$role->id}}" name="{{$role->name}}"
#if(auth()->check())
#foreach(auth()->user()->roles as $userRole)
#if($userRole->id==$role->id) {{"checked"}}
#endif
#endforeach
#endif> {{$role->name}}</input>
#endforeach
Here is controller code
public function edit($id)
{
$role = role::find($id);
$permissions = Permission::all();
return view('admin.role.edit',compact('role','permissions'));
}
And in view
<div>
<label for="name">Assigned Permissions</label>
#foreach ($permissions as $permission)
#if ($permission->for == 'user')
<div class="checkbox">
<label><input type="checkbox" name='permission[]' value="{{ $permission->id }}"
#foreach ($role->permissions as $give_role)
#if ($give_role->id == $permission->id)
checked
#endif
#endforeach
>{{ $permission->name }}</label>
</div>
#endif
#endforeach
</div>

Resources