Laravel - Highlighting roles which are already assigned to a user - laravel

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>

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 display user selected countries with coma separation?

FORM
<div class="form-group mb-3">
<label>Country:</label>
<div class="col-sm-4">
<select id="country-dd" name="country[]" class="js-example-basic-multiple form-control" multiple="multiple">
<option value="">Select Country</option>
#foreach ($countries as $data)
<option value="{{$data->id}}">
{{$data->name}}
</option>
#endforeach
</select>
#if ($errors->has('country'))
<span class="text-danger">{{ $errors->first('country') }}</span>
#endif
</div>
</div>
BLADE FILE
<tbody>
#foreach($users as $user)
<tr>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->countries->name}}</td>
<td>{{$user->states->name}}</td>
<td>{{$user->cities->name}}</td>
<td>{{$user->roles->name}}</td>
<td><button>EDIT</button></td>
<td><button class="deleteuser" data-id="{{ $user->id }}" data-token="{{ csrf_token() }}" >DELETE</button>
</td>
</tr>
#endforeach
</tbody>
CONTROLLER
public function viewuser(){
$this->authorize('Admin');
$users = User::with(['roles', 'countries','states', 'cities'])->get();
// dd($users);
$roles = Role::all();
$countries = Country::all();
$states = State::all();
$cities = City::all();
return view('viewuser', compact('users', 'roles', 'countries', 'states', 'cities'));
}
ROUTE
Route::get('/viewuser',[UserController::class, 'viewuser'])->name('viewuser');
If user selected 2 countries then both countries should get displayed on user listing table separated with coma. Please help me with the same. Thanks in advance.
NOTE: I have not defined foreign key for country in users table.

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

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

Laravel Dropdown Selected item send to edit view

This is my view index page
<div class="form-group">
{!! Form::label('cname','Clients: ') !!}
{!! Form::select('cname',[''=>'Select Category']+$client,null,['class'=>'form-control selectpicker']) !!}
<span class="input-group-btn">
<a class="btn btn-primary" href="">Edit</a>
</span>
</div>
Controller:
public function index() {
$client = Client::pluck('cname','clientid')->all();
return view('client.index', compact('client'));
}
How can i get select item from dropdown and from button click send the selected details to edit view?
You can do something like this
// Controller
public function index()
{
$roles = Roles::all();
$selectedRole = User::first()->role_id;
return view('my_view', compact('roles', 'selectedRole');
}
And then in your view
<select class="form-control m-bot15" name="role_id">
#if ($roles->count())
#foreach($roles as $role)
<option value="{{ $role->id }}" {{ $selectedRole == $role->id ? 'selected="selected"' : '' }}>{{ $role->name }}</option>
#endif
</select>

Input::get() retrieving variable unique id instead of value laravel

I created a form and have a dropdown option which shows a list of option, after selecting the selected value it gets the description value instead. However I would like to retrieve the unique id instead of the description, how can I do so?
In my view.blade.php,
<div class="form-group">
<label class="control-label col-sm-3" for="role_id">Role:</label>
<div class="col-sm-5">
<select class="form-control" id="role_id" name="role_id">
#foreach ($roles as $role)
<option>{{ $role->role_description }}</option>
#endforeach
</select>
</div>
</div>
Then in my controller,
// create the data for our user
$user = new User;
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->mobile = Input::get('mobile');
$user->role_id = Input::get('role_id');
// save our user
$user->save();
Is this part of the code sufficient enough for my explanation? Or shoould I show you the entire code instead.
You have to add a value attribute to your option tags:
#foreach ($roles as $role)
<option value="{{ $role->id }}">{{ $role->role_description }}</option>
#endforeach
And it will return you selected role id.

Resources