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
Related
I'm trying to put a condition to spawn a switch toggled on under certain events, but when I livewire:model the checkbox always spawn toggled off. My blade.php:
<?php
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
$r = $this->role;
$gestion = $r->hasPermissionTo('gestionar');
$consultas = $r->hasPermissionTo('consultar');
$adPe = $r->hasPermissionTo('administrarPerfiles');
$adUs = $r->hasPermissionTo('administrarUsuarios');
?>
<div class="row">
<div class="form-check form-switch col-6 pl-2">
<input class="form-check-input" type="checkbox" wire:model="role.Gestion" id="gest"
#if($gestion){
checked
}
#endif>
<label class="form-check-label" for="gest">GestiĆ³n</label>
</div>
<div class="form-check form-switch col-6">
<input class="form-check-input" type="checkbox" id="cons" wire:model="role.Consulta"
#if($consultas){
checked
}
#endif>
<label class="form-check-label" for="cons">Consultas</label>
</div>
</div>
What should I do in order to solve this?
It is enough just to bind model to checkbox with wire:model. No need for manually setting the checked attribute.
Don't put your logic in the blade file. Set the variable in your Livewire component and bind to that variable with wire:model.
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 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
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>
I'm building a task list where I can add and delete fields dynamically for each user. The problem I have, is saving the data from the checkbox.
I'm using the following code:
<form>
#foreach($tasks as $task)
<div class="col-md-9">
<input id="taskfield" name="dynamic[]" value="{{ $task->name }}" class="form-control field" type="text">
</div>
<div class="col-md-3">
<div class="slideOne">
<input type="checkbox" value="1" id="slideOne" name="check[]" #if($task->finished == 1) checked #endif/>
<label for="slideOne"></label>
</div>
</div>
#endforeach
/*The JS will add the new items here. JS example:*/
<div class="col-md-9">
<input id="taskfield" name="dynamic[]" value="{{ $task->name }}" class="form-control field" type="text">
</div>
<div class="col-md-3">
<div class="slideOne">
<input type="checkbox" value="1" id="slideOne" name="check[]" #if($task->finished == 1) checked #endif/>
<label for="slideOne"></label>
</div>
</div>
</form>
For saving it dynamically, I'm using this in my controller:
DB::table('tasks')->where('user_id', '=', $_POST['uid'])->where('week', '=', $_POST['week'])->where('year', '=', $_POST['year'])->delete();
if(isset($_POST['dynamic'])){
$takenvdweek = $_POST['dynamic'];
$finished = $_POST['check'];
var_dump($finished);
foreach($takenvdweek as $key => $value) {
DB::insert('insert into tasks (name, finished, user_id, week, year) values (?, ?, ?, ?, ?)', array($value, $finished[$key], $_POST['uid'], $_POST['week'], $_POST['year']));
}
}
So my Saving method requires always a value for the checkbox, 0 when unchecked and 1 when checked. I tried it with a hidden field with a 0, but it didn't work because the 0 was always in the POST array. Also when I click the a label, the first foreach label is changing, not the second where I click on. I tried to fix this with a class, but was no success.
Edit:
The foreach is for tasks that exists, but I can also add new items with a piece op JS, they are also inside the form, so I can't give them a foreach-id. I updated my question with the JS example.
Try to set the task id as index of dynamic and check
See below
#foreach($tasks as $task)
<div class="col-md-9">
<input id="taskfield" name="dynamic[$task->id]" value="{{ $task->name }}" class="form-control field" type="text">
</div>
<div class="col-md-3">
<div class="slideOne">
<input type="checkbox" value="1" id="slideOne" name="check[$task->id]" #if($task->finished == 1) checked #endif/>
<label for="slideOne"></label>
</div>
</div>
#endforeach
--Edit---
if task id is not available then you can set index like below
$index = 0;
#foreach($tasks as $task)
<div class="col-md-9">
<input id="taskfield" name="dynamic[$index]" value="{{ $task->name }}" class="form-control field" type="text">
</div>
<div class="col-md-3">
<div class="slideOne">
<input type="checkbox" value="1" id="slideOne" name="check[$index]" #if($task->finished == 1) checked #endif/>
<label for="slideOne"></label>
</div>
</div>
$index++;
#endforeach
This should do:
#foreach($clubs as $club)
<input type="checkbox"
id="{{$club->getCode()}}"
name="clubs[]"
value="{{$club->getId()}}" /> {{$club->getName()}}<br>
#endforeach