Laravel Checkbox not getting set correctly - laravel

Given the following code, all my checkboxes (roles) are marked as checked, even though the user only has one role.
Using Laravel and Spatie Laravel Permissions Package.
I tried the same code in Tinker and it comes back with True, False, False, so it should be working...
#foreach ($roles as $role)
<div>
<label>
<input type="checkbox" value="{{ $role->name }}" checked="{{ $user->hasRole($role->name) ? 'checked' : '' }}">
<span>
{{ $role->name }}
</span>
</label>
</div>
#endforeach

Change your input element to:
<input type="checkbox" value="{{ $role->name }}" {{ $user->hasRole($role->name) ? 'checked' : '' }}>
Checkboxes have a simple checked attribute, you don't assign a value to it.
W3 Example

Related

The update does not return the selected checkbox

I'm having an error updating the checkbox when I return to the view containing it; in particular it always returns me the first checked checkbox but not the values selected before the update.
Can anyone help me to solve this problem?
<div class="mb-3">
<div for="treatment" class="form-label">All treatments</div>
#foreach ($treatments as $treatment)
<input type="checkbox" id="{{$treatment->title}}" name="treatments[]" value="{{$treatment->id}}" {{ $treatment->id == 1 ? 'checked' : null }}>
<label for="{{$treatment->title}}"> {{$treatment->title}}</label><br>
#endforeach
</div>
{{ $treatment->id == 1 ? 'checked' : null }}
In this code, you only added 'checked' attribute if the $treatment->id is 1, so only the first checkbox will be checked.
#foreach ($treatments as $key => $treatment)
<input type="checkbox" id="{{$treatment->title}}" name="treatments[]" value="{{$treatment->id}}" {{ $treatment->id == $key ? 'checked' : null }}>
<label for="{{$treatment->title}}"> {{$treatment->title}}</label><br>
#endforeach
I solved the following:
<div class="mb-3">
<div for="treatment" class="form-label">All treatments</div>
#foreach ($treatments as $treatment)
<input type="checkbox" id="{{$treatment->title}}" name="treatments[]" value="{{$treatment->id}}" #if (count($quote->treatments->where('id', $treatment->id))) checked #endif>
<label for="{{$treatment->title}}"> {{$treatment->title}}</label><br>
#endforeach
</div>

Laravel old values plus newly checked values in edit template

In my edit.blade, I would like to show categories that were checked before by user + user can check new categories. So if a validation error occurred, the newly checked categories do not become unchecked.
This is what I have now:
#foreach($categories as $category)
<input type="checkbox" id="category_id" name="category_id[]" value="{{ $category->id }}"
{{ $category->posts->contains($post->id) ? 'checked' : '' }}
#if (old('category')==$category->id) ? 'checked' : '' #endif>
<label for="{{$category->id}}"> {{$category->category_name}}</label>
#endforeach
How can I do it?
put the previously selected items in an array in controller and pass it to the view. like
$previouslySelected = [1, 3, 5];
and then you use the old helper to show the selected options.
<input type="checkbox" name="category_id[]" value="{{ $category->id }}"
class="form-check-input" id="category{{ $category->id }}"
#if (in_array($category->id, old('category_id', $previouslySelected ))) checked #endif>
this will first check the values in $previouslySelected array. and when the validation fails, will check the values from old values.

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 get the last inserted row with foreach in my blade?

I'm using laravel 5.8 i have two tables askquestions and Responses. When a user post a
question passing in the front-end, i notify all the back-end agent peer mail with the link they access to the show.blade.php here a button to redirect to the Response Form but in this form i'm listing all asked question with a #foreach but i just want to show the last asked question. Need help
<form method="POST" action="{{ route("admin.respones.store") }}" enctype="multipart/form-data">
#csrf
<div class="form-group {{ $errors->has('ask_question') ? 'has-error' : '' }}">
<label class="required" for="ask_question_id"><strong>
{{ trans('La question est:') }}</strong></label>
<select class="form-control select2" name="ask_question_id" id="ask_question_id" required>
#foreach($ask_questions as $id => $ask_question)
<option value="{{ $id }}" {{ old('ask_question_id') == $id ? 'selected' : '' }}>
{{ $ask_question }}</option>
#endforeach
</select>
#if($errors->has('ask_question_id'))
<span class="help-block" role="alert">
{{ $errors->first('ask_question_id') }}
</span>
#endif
<span class="help-block">
{{ trans('') }}
</span>
</div>
public function create()
{
abort_if(Gate::denies('respone_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$categories = Category::all()->pluck('name', 'id')->prepend(trans('Sélectionnez la thématique'), '');
$author_emails = User::all()->pluck('email', 'id')->prepend(trans('Choisissez votre email'), '');
$ask_questions = AskQuestion::all()->pluck('text_question', 'id');
return view('admin.respones.create', compact('categories', 'author_emails', 'ask_questions'));
}

Getting selected checkbox from database - laravel

I am able to save checkbox values into my database. When i am fetching, i try to get the selected checkbox as below but nothing is selected.. What could i be doing wrong in my code please ?
View
<ul class="list-unstyled mb-0">
#foreach($permission as $value)
<li>
<label class="fancy-checkbox mb-0">
<input type="checkbox" in_array($value->id, $rolePermissions) ? true : false value="{{$value->id}}" name="permission[]">
<span>{{ $value->display_name }}</span>
-
<span>{{ $value->description }}</span>
</label>
<hr>
</li>
#endforeach
</ul>
You're missing the .blade syntax, and you should use 'checked' instead of true : false:
<input type="checkbox" value="{{ $value->id }}" name="permission[]" {{ in_array($value->id, $rolePermissions) ? 'checked' : '' }}/>
For checkboxes, simply setting checked is all that is required to determine if it should be initially checked or not.

Resources