I have the following code that works with the Form Collective package, however this isn't working right now as the package hasn't been updated for 5.5. I am also using Spatie's Laravel Permission package
The code I have is
#foreach ($permissions as $permission)
{{Form::checkbox('permissions[]', $permission->id, $role->permissions ) }}
{{Form::label($permission->name, ucfirst($permission->name)) }}<br>
#endforeach
Which I believe is just looping through the permissions and if the permission belongs to the current role check the box.
How can I achieve this without using the package?
I have currently tried
#foreach ($permissions as $permission)
<div class="checkbox">
<label>
{{ ucfirst($permission->name) }}
</label>
<input type="checkbox" name="permissions[]" value="{{ $permission->id }}">
<br>
</div>
#endforeach
But I'm not sure how to attach the checked attribute based on whether the role has a permission in the list.
Just add the checked attribute to the checkbox HTML:
<input type="checkbox" name="permissions[]" value="{{ $permission->id }}" checked>
if you need to set it upon a condition use the following code:
<input type="checkbox" name="permissions[]" value="{{ $permission->id }}" #if(/* some condition */) checked #endif>
EDIT
Since i didn't understand the question before i'll add some details.
Assuming that your Role model has a collection of attached permissions and it is stored in the attribute $role->permissions you could do
<input type="checkbox" name="permissions[]" value="{{ $permission->id }}" #if($role->permissions->contains($permission)) checked #endif>
That way you can check if your role has the permission with id $permission->id.
Try this:
#foreach ($permissions as $permission)
<div class="checkbox">
<label>
{{ ucfirst($permission->name) }}
</label>
<input type="checkbox" name="permissions[]" value="{{ $permission->id }} {{($role->permissions == $permission->id) ? 'checked' : ''}}">
<br>
</div>
#endforeach
Related
I have problem returning 'checked' on my modal, here's the problem
I have these tabel called role_has_permissions that have permission_id that got foreign key from permission tabel.
*note: a Role could have many permissions like this:
In controller I already success returning permissions as usual, but the problem is when I tried to match the id value from permissions tabel with role_has_permissions
In Controller I do like this..
$permissions = DB::table('permissions')->get();
$role_permissions = DB::table('role_has_permissions')->join('permissions', 'role_has_permissions.permission_id', '=', 'permissions.id')->get();
return view('role.index', compact('role', 'permissions', 'role_permissions'));
In view:
#foreach ($permissions as $i)
#foreach ($role_permissions as $item)
<div class="form-check ps-1 mt-2">
<input {{ $i->id == $item->permission_id ? 'checked' : '' }} disabled id="{{ $i->id }}" name="permissions_id[]" class="form-check-input form-disabled" type="checkbox" value="{{ $i->id }}">
<label class="form-check-label" for="{{ $i->id }}">{{ $i->name }}</label>
</div>
#endforeach
#endforeach
The result is not like what I expected. how do I have to do this properly?
multiple foreach should not use in this case for eg:
UserListPermission
UserCreatePermission
the first foreach will loop two times
Admin->UserListPermission
The inner loop will check roles two times.
$admin->UserListPermission == UserListPermission
$admin->UserListPermission == UserCreatePermission
which is no of use instead of that we can get role permissions Id array as below
$permissions = DB::table('permissions')->get();
$role_permissions = DB::table('role_has_permissions')->join('permissions', 'role_has_permissions.permission_id', '=', 'permissions.id')->pluck('permission_id')->toArray();
return view('role.index', compact('role', 'permissions', 'role_permissions'));
and on the view page:
#foreach ($permissions as $i)
#php $checked = in_array($i->id, $role_permissions) ? 'checked' : ''; #endphp
<div class="form-check ps-1 mt-2">
<input {{ $checked }} disabled id="{{ $i->id }}" name="permissions_id[]" class="form-check-input form-disabled" type="checkbox" value="{{ $i->id }}">
<label class="form-check-label" for="{{ $i->id }}">{{ $i->name }}</label>
</div>
#endforeach
Hope it will be helpful.
I have a form where you can check filters which will later filter a search query. The form is working but I would love to mark the checked checkboxes after the request as checked that the user can see what he selected.
It's working with one checkbox but if I add an array it's not working anymore.
<input type="text" class="form-control bg-transparent border-0 searchbox" placeholder="{{ __('general.search_placeholder') }}" value="{{ app('request')->input('q') }}" name="q" aria-label="Search" aria-describedby="button-addon2">
<div class="input-group-append">
<button class="btn border-0 searchbox" type="submit " id="button-addon2"><i class="fas fa-search"></i></button>
</div>
#foreach($collections as $collection)
<div class="form-check">
<input class="form-check-input" type="checkbox" value="{{$collection->id}}" name="s[]" id="defaultCheck{{$collection->id}}" {{ app('request')->input('s') == $collection->id ? 'checked' : '' }}>
<label class="form-check-label" for="defaultCheck{{$collection->id}}">
{{$collection->id}}
</label>
</div>
#endforeach
</form>```
Does anybody know how to solve that?
If s[] is an array of checked checkboxes, you could use in_array to test if the current $collection->id is in there:
{{ in_array($collection->id, app('request')->input('s')) ? 'checked' : '' }}
I have two tables; both of which have several columns. In one, I have all licenses the user can select (with checkboxes), in the other I store what licenses the user has.
I created a model to get all licenses, and model to get what licenses the user has.
Now, I can't understand how to create a view of all licences where those that the user already has are already checked - e.g when I create the form with these checkboxes, how can I check if the user already has the license.
I can get values but I cannot get the #if syntax work.
Here is my code currently:
<div class="form-group col-sm-12">
<div class="form-check form-check-inline">
#foreach($all_license as $all_licen_row)
#foreach($drive_licenses as $lisen)
#if($lisen->license_id==$all_licen_row->id)
<input class="form-check-input" type="checkbox"
name="{{$all_licen_row->license_id}}" checked>
<label class="form-check-label"
for="inlineCheckbox1">{{ $all_licen_row->class }}</label>)
#else
<input class="form-check-input" type="checkbox" name="{{$all_licen_row->license_id}}">
<label class="form-check-label"
for="inlineCheckbox1">{{ $all_licen_row->class }}</label>)
#endif
#endforeach
#if($errors->has('id'))
<span class="help-block">
<strong class="text-danger">{{ $errors->first('drive_licence') }}</strong>
</span>
#endif
#endforeach
</div>
</div>
Something like this is usually easier to handle without using an inner loop. You can check which id's should be selected before looping through $all_license by just storing the ids from drive_licenses into an array and simply check if the $all_license id exists in the array. Example:
<?php
$ids = array();
foreach($drive_licenses as $lisen) {
array_push($ids, $lisen->license_id)
}
?>
#foreach($all_license as $all_licen_row)
#if(in_array($all_licen_row->id, $ids))
<input class="form-check-input" type="checkbox" name="{{$all_licen_row->license_id}}" checked>
<label class="form-check-label" for="inlineCheckbox1">{{ $all_licen_row->class }}</label>
#else
...
#endif
#endforeach
If you want, you could also use the ternary operator (e.g., (?:)) to shorten your code some. Example:
#foreach($all_license as $all_licen_row)
<input class="form-check-input" type="checkbox" name="{{$all_licen_row->license_id}}"{{ (in_array($all_licen_row->id, $ids) ? ' checked' : '') }}>
<label class="form-check-label" for="inlineCheckbox1">{{ $all_licen_row->class }}</label>
#endforeach
I am working in Roles and Permission.
I have fetched all permission from permissions table. I want to show all available permission to superadmin and at the same time if he has already that some permission then checkbox should be checked ,if some are not, checkbox should not checked for them
Permisson table has field:id,name,display_name,description,timestamp
roles table:id,name,display_name,description,timestamp
permission_user table:permission_id,user_id
Here is current state :http://imgur.com/a/bfveP
Rolecontroller:
public function edit($id)
{
$role = Role::where('id', $id)->with('permissions')->first();
$permissions = Permission::all();
// dd($role);//i am getting permission of that role
return view('manage.roles.edit')->with('permissions',$permissions)- >with('role',$role);
}
edit.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Edit</div>
<div class="panel-body">
form...
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Edit</div>
<div class="panel-body">
#foreach( $permissions as $permission )
<p>
<input type="checkbox"
name="permissions[]"
id="perm_{{ $permission->id }}"
{{ in_array($permission->id, $role->permissions->pluck('id') ) ? 'checked="checked"' : '' }}
value="{{ $permission->id }}">
{{ $permission->display_name }}
</p>
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
Change
{{$role->display_name?'checked="checked"':''}}"
To
{{$role->display_name }}" {{Auth::user()->can($permission) ?'checked="checked"':''}}>
This will give you the following code snippet
<p>
<input type="checkbox" name="" id="{{$role->display_name }}" {{Auth::user()->can($permission) ?'checked="checked"':''}}>
{{$permission->display_name}}
</p>
This will set the id to the display name and use a coalesce operator to check if the current authenticated user (or whatever user you change that to) has the role $role.
The code from #Milo should work, but my guess is it's not working because it isn't connected to users..
Instead of Auth::user()->can($permission), try to make a method on the role model that returns a Boolean if the role contains that permission parameter. Something like $role->contains($permission).
Then do something like list the id and name(careful not to put checked in them), then afterwards, I usually do something like #if($role->contains($permission)) checked="checked" #endif
First create method to check is the permission already add in role model
public function isPermissionExist($id)
{
foreach($this->permissions as $permission){
if($permission->id == $id)
return true;
}
return false;
}
next in view
#foreach( $permissions as $permission )
<p>
<input type="checkbox"
name="permissions[]"
id="perm_{{ $permission->id }}"
#if($role->isPermissionExist($permission->id)) checked #endif
value="{{ $permission->id }}">
{{ $permission->display_name }}
</p>
#endforeach
Try this in your edit.blade.php
<label class="checkbox-inline "for="perm[{{ $permission->id }}]">
<input id="perm[{{ $permission->id }}]" name="perm[{{ $permission->id }}]" type="checkbox" value="{{ $permission->id }}"
#if($role->permissions->contains($permission->id)) checked=checked #endif
> {{ $permission->name }}
</label>
I am trying to create an update page and i've completed most of the page, with exception to the checkbox section. For some reason that i've yet to figure out, the checkboxes are duplicated. I am using laravel.
This is the code for that particular section of the form.
<div class="form-group">
<label>Focus Area</label>
<br>
#foreach(FocusArea::all() as $focusArea)
#if(isset($project))
<div class="checkbox material checkbox-success">
<label>
#foreach($project->getIdsOfFocusAreas() as $selectedFocusArea)
#if($selectedFocusArea == $focusArea->focus_area_id)
<input type="checkbox" name="focus-area[]" value="{{ $selectedFocusArea }}" checked>
#else
<input type="checkbox" name="focus-area[]" value="{{ $selectedFocusArea }}">
#endif
#endforeach
{{ $focusArea->name }}
</label>
</div>
<br>
#endif
#endforeach
</div>
Some extra information:
The number of elements in the array generated by FocusArea::all() is 5.
The number of elements in the array generated by getIdsOfFocusAreas() is 2.
I know that it is duplicating twice because of point number 2, im just not exactly sure why it is duplicating in the first place.
Try this:
<div class="form-group">
<label>Focus Area</label>
<br>
<?php $selectedFlug = 0; ?>
#foreach(FocusArea::all() as $focusArea)
#if(isset($project))
<div class="checkbox material checkbox-success">
<label>
#foreach($project->getIdsOfFocusAreas() as $selectedFocusArea)
#if($selectedFocusArea == $focusArea->focus_area_id)
<input type="checkbox" name="focus-area[]" value="{{ $selectedFocusArea }}" {{ ($selectedFocusArea == $focusArea->focus_area_id) ? 'checked' : '' }} >
<?php $selectedFlug = 1; ?>
#break
#else
<?php $selectedFlug = 0; ?>
#endif
#endforeach
#if($selectedFlug == 0)
<input type="checkbox" name="focus-area[]" value="{{ $focusArea->focus_area_id }}">
#endif
{{ $focusArea->name }}
</label>
</div>
<br>
#endif
#endforeach
</div>