Laravel Livewire isn't applying "checked" property in form checkbox - php

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.

Related

Check only one checkbox and uncheck others using livewire

I'm trying to check only one checkbox at a time and others uncheck, but when I want to check a new checkbox then the previous will uncheck, and so on.
my code in blade:
#foreach($addressEmployer as $address)
<div class="col-12 col-lg-3 p-2 m-2 rounded" style="border: dashed #a1a1a1;">
<label for="check{{$address->id}}"></label>
<div class="row">
<div class="col-2 mt-5">
<input wire:model="addressSelected.{{$address->id}}"
value="{{$address->id}}"
class="form-check-input" type="checkbox"
id="check{{$address->id}}">
</div>
<div class="col-10">
<p> {{$address->province->name}} - {{$address->city->name}}</p>
<p> {{$address->address}}</p>
<a wire:click="setAddress({{$address->id}})" class="float-end"
data-bs-toggle="modal"
href="#editAddressModal"
role="button">{{__('Edit')}}</a>
<a wire:click="$emit('addressId',{{$address->id}})" class=" me-3 float-end"
data-bs-toggle="modal"
href="#deleteAddressModal"
role="button">{{__('Delete')}}</a>
</div>
</div>
</div>
#endforeach
I read the addresses from the database and display them with foreach and I want to select one of the displayed addresses.
I am looking for the right solution to this issue. Thanks if you have a solution.
When you have multiple options but want to limit selection to a single option, radio buttons are your friend.
If the previously selected radio button is not de-selecting, it suggests you haven't grouped your radio button elements correctly using the name attribute.
Here is a simplified example to get you on the right path.
Component
class AddressComponent extends Component
{
public $addresses = Address::all();
public $selectedAddressId;
public function mount()
{
$this->addresses = Address::all();
$this->selectedAddressId = 1;
}
public function render()
{
return view('livewire.address-component-view');
}
}
Component view
<div>
#foreach ($addresses as $address)
<div class="mt-1">
<input type="radio" id="{{ $address->id }}" name="address" value="{{ $address->id }}" wire:model="selectedAddressId" />
<label for="{{ $address->id }}">{{ $address->address }}</label>
</div>
#endforeach
<h3>Selected Address ID: {{ $selectedAddressId }}</h3>
</div>
I'm not shure why you're trying this with checkbox, instead of radiobutton. But maybe you can handle something like this
public $addressSelected = [];
protected $rules = [
'addressSelected.id' => 'nullable'
];
public function updatingAddressSelected($value)
{
$this->addressSelected = [];
}
// in blade add the wire:key directive to checkbox's root div
<div class="col-2 mt-5" wire:key="checkbox-id-{{ $address->id }}">
<input wire:model="addressSelected.{{$address->id}}"
value="{{$address->id}}"
class="form-check-input" type="checkbox"
id="check{{$address->id}}"
#if(in_array($address->id,$addressSelected)) checked #endif
>
</div>

laravel 5 - change label id though a foreach

I have a foreach loop that I want it to create some unique checkboxes.
because I am hiding the checkbox and using label to check the checkbox (making checkbox as div) I want my checkbox to have a unique id with the label the same how can I achieve that ?? here is my code :
#foreach($pdate as $pdates)
<div id="checkboxes" class="col-lg-2 text-center">
<input type="checkbox" name="rGroup" value="{{verta($pdates->date)->format('Y/m/d')}}" id="r1"/>
<label class="whatever" for="r1"> {{verta($pdates->date)->format('Y/m/d')}}
<hr>
{{$pdates->price}}</label>
</div>
#endforeach
So here I want id and for to be rl+1 and so on untill my foreach finishes.
You can use an index like this:
#foreach($pdate as $index => $pdates)
<div id="checkboxes" class="col-lg-2 text-center">
<input type="checkbox" name="rGroup" value="{{verta($pdates->date)->format('Y/m/d')}}" id="r{{$index}}"/>
<label class="whatever" for="r1">
{{verta($pdates->date)->format('Y/m/d')}}
<hr>
{{$pdates->price}}
</label>
</div>
#endforeach
So your checkboxes ids will be r0, r1, r2...
You can use counter to make unique id like this:
<?php $counrer=1; ?>
#foreach($pdate as $pdates)
<div id="checkboxes" class="col-lg-2 text-center">
<input type="checkbox" name="rGroup" value="{{verta($pdates->date)->format('Y/m/d')}}" id="r1{{$counter}}"/>
<label class="whatever" for="r1"> {{verta($pdates->date)->format('Y/m/d')}}
<hr>
{{$pdates->price}}</label>
</div>
<?php $counter++; ?>
#endforeach

Laravel form checkbox data

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

How can I make a checkbox checked or not checked depending on what the value is in the database

So, I have this in my edit.blade.php
<div class="form-check disabled">
<label class="form-check-label">
<input class="form-check-input" type="hidden" value='0' name="is_default">
<input class="form-check-input" type="checkbox" value='1' name="is_default">
Default Variant
</label>
</div>
and I have an is_default column in my database which has the values of either 0 or 1 depending if the checkbox is checked or not.
Now, I want to create an edit page and I want to show if that if is_default value is 1 then the checkbox should be checked, else it will be unchecked.
the value of is_default is represented as value="{{ $variant->is_default }}"
Let's try this
In edit.blade.php
<div class="form-check disabled">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" value='1' name="is_default" {{ (isset($variant->is_default) && !empty($variant->is_default))?'checked':'' }}>
Default Variant
</label>
</div>
in controller check for
<?php
.....
$is_default = isset($_POST['is_default'])?1:0;
// If checkbox is checked then isset will become true else false
// Update the db
....
?>
I hope this will help you... Happy Coding:)

Check the correct checkbox according to value fetched from database

I am using codeigniter for a project. On the html side, I have 3 checkboxes in which I will see which is the checked one and store the value in database. Now I want to fetch the value and checked the corresponding checkbox.
My checkboxes is as below
<div class="span2">
<label class="radio">
<input class="attrInputs" type="radio" name="shoulder" value="flat">
Flat
</label>
<img src="http://placehold.it/126x126/cbcbcb" class="push-top">
</div>
<div class="span2">
<label class="radio">
<input class="attrInputs" type="radio" name="shoulder" value="regular">
Regular
</label>
<img src="http://placehold.it/126x126/cbcbcb" class="push-top">
</div>
<div class="span2 border-right">
<label class="radio">
<input class="attrInputs" type="radio" name="shoulder" value="sloped">
Sloped
</label>
<img src="http://placehold.it/126x126/cbcbcb" class="push-top">
</div>
So for example if I checked input element with value sloped, the value sloped will be stored in the database, when user logs in, its preloaded with its checked input of the value sloped
Thanks in advance guys!
Okay without your controller and how you're returning the data I am going to give you the basics of how this works. Essentially you're going to be checking the value of shoulder and determining the appropriate box. So in your controller you'd send data to the view something like this (again I have no idea what your db or tables looks like so this is example only).
Controller:
$this->load->model('someModel');
//the following populates the formData variable with an array from your database.
//I am going to assume you know how to do this.
$data['formData'] = $this->someModel->getData();
$this->load->view('someView',$data);
View, while it can be easier to use CI's built in form handler it's not necessary so I'll just use your code for example.
<div class="span2">
<label class="radio">
<input class="attrInputs" type="radio" name="shoulder" value="flat"
checked="<?=$formdata['shoulder'] == 'flat' ? 'checked' : '' ;?>">
Flat
</label>
<img src="http://placehold.it/126x126/cbcbcb" class="push-top">
</div>
<div class="span2">
<label class="radio">
<input class="attrInputs" type="radio" name="shoulder" value="regular"
checked="<?=$formdata['shoulder'] == 'regular' ? 'checked' : '' ;?>">
Regular
</label>
<img src="http://placehold.it/126x126/cbcbcb" class="push-top">
</div>
<div class="span2 border-right">
<label class="radio">
<input class="attrInputs" type="radio" name="shoulder" value="sloped"
checked="<?=$formdata['shoulder'] == 'sloped' ? 'checked' : '' ;?>">
Sloped
</label>
<img src="http://placehold.it/126x126/cbcbcb" class="push-top">
</div>
What the above code is doing is using a shorthand if statement to determine which box should be checked. In each one it checks if the value of 'shoulder' that your database returned is the same as the checkbox and sets it's checked value to checked if it is and blank if it isn't.
It also uses php shorttags, so if those aren't enabled on your server either enable them or adjust the php tags to read:
checked="<?php echo ($formdata['shoulder'] == 'flat' ? 'checked' : '') ;?>"

Categories