Checkbox selection with two foreach loops - laravel

I have below code for showing all taxes as checkboxes,
I am using same form for editing taxes,
#foreach($taxes as $t)
#foreach($grouptax_taxes as $tg)
<label class="checkbox">
<input type="checkbox" name="taxgroup_rate[]" {{ isset($taxgroup) && $t->tax_id == $tg->tax_id ? 'checked' : ''}} id="taxgroup_{{$t->tax_id}}" value="{{$t->tax_id}}">
<span></span>{{$t->tax_name}} - {{$t->tax_rate}} &percnt; </label>
#endforeach
#endforeach
Problem is that it repeats all taxes multiple times as grouptax_rate, I know it will do repeat, but not sure how to select only those checkboxes which values are in $grouptax_taxes and remaining $taxes should not be selected,
How can I do that?

For the checked if values are equal should be done inside the checkbox, i.e the foreach loop should only cover the checked
#foreach($taxes as $t)
<label class="checkbox">
<input type="checkbox" name="taxgroup_rate[]" value="{{$t->tax_id}}" id="taxgroup_{{$t->tax_id}}"
#foreach($grouptax_taxes as $tg)
{{ ($t->tax_id == $tg->tax_id)? 'checked' : ''}}
#endforeach >
<span>{{$t->tax_name}} - {{$t->tax_rate}}</span></label>
#endforeach

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.

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.

passing a complete loop to bootstrap modal

I want to edit multi select for which i want to pass two loops to
bootstrap modal and their i want to compare id's and on that basis i will select already selected record my button
<a type="button" data-toggle="modal" data-target="#editModal{{ $value->id}}">
<span class="glyphicon glyphicon-edit"></span>
</a>
my form
<div class="form-group">
<select name="upsubject[]" multiple class="form-control">
#foreach($subject as $data)
#foreach($value->subject as $key)
<option value="{{$data->id}}" <?php if ($key->id == $data->id)
{echo
"selected";}
?>>{{$data->name}}</option>
#endforeach
#endforeach
</select>
</div>`
try this:
<option {{ ($key->id == $data->id) ? 'selected' : '' }}>{{ $data->name }}</option>
I have to detach the selected and than select new records as i am updating subjects but the above logic give me same subject three times each

Loop through collection - target every item above a certain number

I'm looping through my collection to display all my data. Pretty standard stuff - nothing special.
I wan't to target and apply functionality to every element from number 9 onwards.
For example. The first 8 sessions are free, after that I want to append a button to the 9th+ ones to pay.
#foreach($sessions as $session)
<input type="text" name="event" value="{{ $counsellingSession->event_start_time }}" class="form-control datetimepicker" >
#endforeach
could i apply a class to all items in position 9 and above.
You could use the magic $loop variable Blade templates have in #foreach loops like this:
#foreach($sessions as $session)
#if($loop->index > 8)
<!-- Display payment button -->
#endif
#endforeach
Documentation: https://laravel.com/docs/master/blade#loops
you can do the following:
$count=0;
#foreach($sessions as $session)
$count++;
#if($count >=9)
here apply your classs
#else
<input type="text" name="event" value="{{ $counsellingSession->event_start_time }}" class="form-control datetimepicker" >
#endif
#endforeach
Simple solution for that is you can use the keys of array in loop,
#foreach($sessions as $key => $session)
#if($key > 8)
<input type="submit">
#endif
<input type="text" name="event" value="{{ $counsellingSession->event_start_time }}"
class="form-control datetimepicker" >
#endforeach

Resources