Laravel old values plus newly checked values in edit template - laravel

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.

Related

Checkbox selection with two foreach loops

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

Laravel select old and new value as selected

Not sure exactly how to ask this question. But how do you select the old value after you fetched the value from the database? When you click on edit, you fetch the database result. But if something goes wrong, I want the value selected that the user inserted.
This is what works if you want to add a value.
<select name="category_id" class="form-control" id="category">
#foreach($category as $cat)
<option value="{{$cat->id}}"
{{ old('category_id', $cat->id) == $subCategory->category_id ? "selected" : "" }}>
{{ $cat->category_en }} | {{ $cat->category_de }}</option>
#endforeach
</select>
However, when I press update, it doesn't use the old selected value, but it automatically goes to the first option in the foreach.
use optional() helper along with the old() helper to handle this kind of situation.
<option value="{{ $cat->id }}" #if (old('category_id', optional($subCategory)->category_id) == $cat->id) selected #endif>{{ $cat->category_en }} | {{ $cat->category_de }}</option>
what this means is when it gets old input value null, it will use the object value to select an item. when there is old input value it will select the old input value. you can omit the optional helper though, it is useful when you are using the same form for create and update.
<select name="category_id" class="form-control" id="category">
#foreach($category as $cat)
<option value="{{$cat->id}}"
#if($cat->id == $subCategory->category_id) ? selected : '' #endif>
{{ $cat->category_en }} | {{ $cat->category_de }}</option>
#endforeach
</select>

Laravel, check if value is in some collection (in blade)

I have 2 collections, $all have all the values (5), and $some has only 2 values. I'm trying to this:
For each value of $all, if that item in the loop is in the $some collection, put the the value of that $some item (something like that) in the input:
#foreach($all as $item)
<div>
#if(in_array($item->id, $some))
<input type="number" value="{{ Here I need to put the value of $some where id of some be teh same of $all }}">
#else
<input type="number" value="0">
#endif
</div>
#endforeach
I don't know how to read and decide all that inside the blade.
EDIT: I was trying something like this:
#if(in_array($item->id, $some))
<input type="number" value="{{ $some->find($item->id)->value }}">
I don't know what the specific fields are on $some, but you can do what you want (replacing the fields you need to compare or show) like this:
#if(in_array($item->id, $some->pluck('id')->toArray()))
<input type="number" value="{{ $some->where('id', $item->id)->first()->fieldYouWant}}">
#else
<input type="number" value="0">
#endif
Basically, pull the $some ids into an array, compare the current looped $item->id (from $all), and if it hits, pull the specific $some object that matched (id from $some, and id from current $all item) and get the value you need from whatever the field you need is called.
In my case, i was trying to do this on a array of checkboxes input, in case someone needs it this way:
<input type="checkbox" name="guides[]" value="{{ $item->id }}"
{{ in_array($item->id, $some->pluck('id')->toArray()) ? 'checked' : '' }} >

Laravel Checkbox not getting set correctly

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

Populate select dropdown if returning with errors

As with my text fields, if an error is returned on the view, I want to populate the select elements with the previously submitted data, like: value="{{ old('element_name_here') }}".
Current view:
<select class="form-control" id="input" name="{{ $questionDetail->question_number }}" required="required">
<option disabled selected value> -- Select an option --</option>
#foreach($answers as $answer)
<option value="{{ $answer->value }}">{{ $answer->question }}</option>
#endforeach
</select>
How can I implement it into the above scenario, as there doesn't seem to be a place where I can add a value without overwriting one? Thank you.
<option
value="{{ $answer->value }}"
{{ $answer->value == old(questionDetail->question_number) ? 'selected' : '' }}>
{{ $answer->question }}
</option>
You can remove 'selected' from the first option. It will be selected by default because it's the first. if old() exists, then it will do the check.
You need to mark previous value as selected so something like this should work:
<select class="form-control" id="input" name="{{ $questionDetail->question_number }}" required="required">
<option disabled #if(!old($questionDetail->question_number)) selected #endif value> -- Select an option --</option>
#foreach($answers as $answer)
<option value="{{ $answer->value }}" #if(old($questionDetail->question_number) == $answer->value) selected #endif>{{ $answer->question }}</option>
#endforeach
</select>
Probably when working a lot with forms it's a good idea to use https://laravelcollective.com/docs/5.3/html (branch 5.4)

Resources