How to create dropdown in laravel - laravel

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>

Related

How to display a select only when other select's option was chosen?-

I have 2 selects on a page and i want to show the 2nd only when in the first select a specific option was chosen, is that possible in laravel? here the code:
<select class="form-select my-2" name="category_id">
<option disabled selected>Seleziona una categoria</option>
#foreach ($categories as $category)
<option value="{{ $category->id }}" #if ($category->id == old('category_id')) selected #endif>
{{ $category->name }}
</option>
#endforeach
</select>
<select class="form-select my-2" name="type_id">
<option disabled selected>Seleziona un tipo di sushi</option>
#foreach ($types as $type)
<option value="{{ $type->id }}" #if ($type->id == old('type_id')) selected #endif>
{{ $type->name }}
</option>
#endforeach
</select>
Using JQuery :
<script>
$('select[name="category_id"]').change(function (){
let categoryId = $(this).val();
if (categoryId == null)
{
$('select[name="type_id"]').hide();
}else{
$('select[name="type_id"]').hide();
}
});
</script>

Old value in multiple select option in laravel

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>

Add attribute to first item of array in Laravel

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>

Select option with selected values returning duplicates

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

Laravel dropbox initial value on update

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

Resources