I am using a JavaScript selectize to allow users to add multiple permissions when creating a role. This part wonderfully.
The challenge comes in when the user would like to edit a certain role. I would like to load the Selectize with the data that they had previously selected when creating the role. Here is my code;
Blade
<select id="permissions" name="permission[]" multiple="multiple">
<option value="">Choose a permission</option>
@foreach($permission as $value)
<option value="{{ $value->name }}">{{ $value->name }}</option>
@endforeach
</select>
JavaScript
<script>
$(function() {
$('#permissions').selectize();
});
</script>
Currently, the selectize loads while empty and I am not sure how to tackle this challenge. Kindly assist.
try this:
<select id="permissions" name="permission[]" multiple="multiple">
<option value="">Choose a permission</option>
@foreach($permission as $value)
<option value="{{ $value->name }}" {{ (in_array($value->name, $permissionsArray)) ? 'selected' : '' }} {{ (collect(old('permission'))->contains($value->name)) ? 'selected':'' }}>{{ $value->name }}</option>
@endforeach
</select>
You can use the in_array function of php to check if the permission is already in selected permissions.
@foreach($permission as $value)
<option {{in_array($value->name, $selectedPermissions) ? 'selected':''}} value="{{ $value->name }}">
{{$value->name}}
</option>
@endforeach
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.