That's code showing deliverer names and if one selected I got his id on db table
<select name="deliverer_id" class="selectpicker form-control">
#foreach($dvs as $dv)
<option value="{{$dv->id}}"> {{ $dv->name }} </option>
#endforeach
</select>
I wanna show the old name of deliverer_id. I try before #if ...
You will need to compare your value in foreach loop with the old submitted value. For that Laravel provides a method to retreive old value from request $request->old('deliverer_id');
Or you can do it in your blade like this
<select name="deliverer_id" class="selectpicker form-control">
#foreach($dvs as $dv)
<option value="{{$dv->id}}" #if($dv->id == old('deliverer_id')) selected #endif > {{ $dv->name }} </option>
#endforeach
</select>
I foud the solution
#foreach($dvs as $dv)
<option value="{{ $dv->id}}"{{ old('deliverer_id', $order->deliverer_id) == $dv->id ? 'selected' : '' }} > {{$dv->name}}
</option>
#endforeach
I'm not sure what you mean but it seems that you want to select a default option based on specific condition in that case you can do as in the following:
<select name="deliverer_id" class="selectpicker form-control">
#foreach($dvs as $dv)
<option value="{{$dv->id}}" {{$dv->id == $old_deliverer_id ?? "selected" : ""}}> {{ $dv->name }}</option>
#endforeach
</select>
Related
I have a dropdown and in edit page I want to show old input. I use this code but it still shows "please select". Where is the problem?
<select wire:model.defer="form.department" id="form.department" class="tf-input">
<option value="null" selected disabled>{{ __('Please select') }}</option>
#foreach($this->departments as $department)
<option {{ $form->department_id == $department->id ? 'selected' : '' }} value="{{ $department->id }}" >{{ $department->name }}</option>
#endforeach
</select>
Livewire does not listen to selected attributes in HTML when you are using wire:model, because it will overwrite it with the value from the component. Therefor, remove it entirely.
<select wire:model.defer="form.department" id="form.department" class="tf-input">
<option value="null" disabled>{{ __('Please select') }}</option>
#foreach ($this->departments as $department)
<option value="{{ $department->id }}" wire:key="department-{{ $department->id }}">{{ $department->name }}</option>
#endforeach
</select>
As you can see, I removed the parts that adds selected, but I also added a wire:key to the options - this is because the elements are rendered in a loop, and will help Livewire keep track of all elements on a page.
To set a value that is bound through wire:model, you need to set the value in the component. So in the mount() method, you can do
$this->form['department'] = $department->id;
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>
Here is my select option
<select name="category[]" id="categories"
class="js-example-basic-single form-control"multiple>
#foreach ($category as $element)
<option value=" {{$element->id}}}"{{ (old("category[]") == $element->id ? "selected":"") }}>
{{$element->name}}
</option>
#endforeach
</select>
I can't get the old value ,What I'm doing wrong?
how can i get the old value ?
thanks.
The variable is not named category[] on the server side, that is just notation to turn it into an array; it is named category.
As Kamlesh Paul has stated you can use in_array to check against the possible values of the old input array:
{{ in_array($element->id, (array) old('category', [])) ? "selected" : "" }}
You could try the following code:
<select name="category[]" id="categories" class="js-example-basic-single form-control"multiple>
#foreach ($category as $element)
<option value=" {{$element->id}} " {{ (collect(old('category'))->contains($element->id)) ? 'selected':'' }}>{{ $element->name }}</option>
#endforeach
</select>
you can try in_array()
https://www.w3schools.com/php/func_array_in_array.asp
<select name="category[]" id="categories" class="js-example-basic-single form-control" multiple>
#foreach ($category as $element)
<option value=" {{$element->id}}}" {{ in_array($element->id ,old("category",[]) ? "selected":"") }}>
{{$element->name}}
</option>
#endforeach
</select>
i want dropdown of one column in Laravel, for creating it should display all nations in drop-down, for update purpose current nation should be selected.
any example or tutorial would be helpful thanks.
My Try:
<select name="nation" class="custom-select" >
<option selected value="">Choose...</option>
<option value="{{#$teacher->nation}}" {{#$teacher->nation== "Pakistan" ? 'selected' : ''}} >{{#$teacher->nation}}</option>
</select>
Problem: my dropdown is empty.
<select name="nation" class="custom-select">
<option selected value="">Choose...</option>
#foreach($teachers as $teacher)
<option value="{{ $teacher->nation }}" {{ $teacher->nation == "Pakistan" ? 'selected' : '' }} >{{ $teacher->nation }}</option>
#endforeach
</select>
Here is the dropdown structure in laravel
<select class="form-control m-bot15" name="role_id">
#if($roles->count() > 0)
#foreach($roles as $role)
<option value="{{$role->id}}">{{$role->name}}</option>
#endForeach
#else
No Record Found
#endif
</select>
<select name="nation" class="custom-select" >
<option value="">Choose...</option>
#foreach ($teachers as $key => $value)
<option value="{{ $key }}" {{ old('nation') == $key ? 'selected' : ''}}>{{ $value }}</option>
#endforeach
</select>
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)