I have a select box and want to get the options of an array.
<select class="form-control select-search" name="user_select_crypto"
id="user_select_crypto">
#foreach($currencies as $currency)
<option value="{{ $currency->id }}">{{ $currency->crypto->name }}</option>
#endforeach
</select>
Now, I want to first item add the selected attribute, how can I do this?
You can use one of 2 things: Either $index => $val approach as classic foreach.
#foreach($currencies as $i => $currency)
<option value="{{ $currency->id }}" {{ $i == 0 ? 'selected' : ''}}>{{$currency->crypto->name}}</option>
#endforeach
Also as of more Laravel way, there is $loop variable you can access inside blade in #foreach
#foreach($currencies as $i => $currency)
<option value="{{ $currency->id }}" {{ $loop->first ? 'selected' : ''}}>{{$currency->crypto->name}}</option>
#endforeach
Of course, you can translate {{ $loop->first ? 'selected' : ''}} into:
#if ($loop->first) 'selected' #endif
A better way to deal with such issues is to add a default <option></option>;
<select class="form-control select-search" name="user_select_crypto" id="user_select_crypto">
<option value="" selected disabled >Select Currency</option>
#foreach($currencies as $key => $currency)
<option value="{{ $currency->id }}">{{$currency->crypto->name}}</option>
#endforeach
</select>
Related
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>
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>
I'm trying to get a old selected value and show it inside a select option, as well as a non selected value, however, when i try to compare then it doesnt seen to work, so far i can only get duplicates. What's the best way to implement it?
That's how my code look like.
Controller:
public function index()
{
$roles = Role::get();
$permissions = Permission::get();
return view('role.index', compact(['roles','permissions']));
}
Role View:
#foreach($role->permission as $permissioninrole)
<option name="permissions[]" {{ old('name', $permissioninrole->name) == $permissioninrole->name ? 'selected' : '' }} value="{{$permissioninrole->id}}">{{$permissioninrole->name}}</option>
#endforeach
#isset($permissioninrole)
#foreach($permissions as $permission)
#if($permissioninrole->name != $permission->name)
<option class="rem" name="permissions[]" value="{{$permission->id}}">{{$permission->name}}</option>
#endif
#endforeach
#endisset
#empty($permissioninrole)
#foreach($permissions as $permission)
<option name="permissions[]" value="{{$permission->id}}">{{$permission->name}}</option>
#endforeach
#endempty
How it looks. HTML
<option name="permissions[]" selected value="15">role-create</option>
<option name="permissions[]" selected value="16">role-read</option>
<option name="permissions[]" value="15">role-create</option>
<option name="permissions[]" value="16">role-read</option>
...
How it should be if role 'x' has permission role-create.
<option name="permissions[]" selected value="15">role-create</option>
<option name="permissions[]" value="16">role-read</option>
...
Solution
#foreach($permissions as $key => $value)
<option name="permissions[]" {{ in_array($key +1, $role->permission()->pluck('id')->toArray()) ? 'selected' : '' }} value="{{$value->id}}">{{ $value->name }}</option>
#endforeach
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>
Items belongs to Categories. On edit page for Items I have an html select with Categories. Is this the best solution to have the right initial state for this select?
<select name="id_category" class="form-control">
#foreach($categories as $category)
#if($category->id == $item->id_category)
<option value="{{ $category->id }}">
{{ $category->name }}
</option>
#endif
#endforeach
#foreach($categories as $category)
#if($category->id != $item->id_category)
<option value="{{ $category->id }}">
{{ $category->name }}
</option>
#endif
#endforeach
</select>
I'm not sure i understand your question, but i think you want to select item was previously select by user, so you can use inline if to set selected.
<select name="id_category" class="form-control">
#foreach($categories as $category)
<option value="{{ $category->id }}" {{ $category->id == $item->id_category ? "selected" : "" }} >
{{ $category->name }}
</option>
#endforeach
</select>
Sorry if i didn't understand your question