I want to keep the values in multiselect however, it shows Undefined offset: 2. The codes is below for the blade file.
<select multiple="multiple" name="warehouseId[]" id="warehouse" class="form-control" style="width:100%;">
#if($warehouseData)
#foreach ($warehouseData as $key => $warehouse)
<option value="{{$warehouse->id}}" >{{$warehouse->name}} {{$adminUserWarehouseSelectedData[$key]->id}}</option>
#endforeach
#endif
</select>
Add an #isset directive
<select multiple="multiple" name="warehouseId[]" id="warehouse" class="form-control" style="width:100%;">
#if($warehouseData)
#foreach ($warehouseData as $key => $warehouse)
#isset($adminUserWarehouseSelectedData[$key]->id)
<option value="{{$warehouse->id}}">
{{$warehouse->name}} {{$adminUserWarehouseSelectedData[$key]->id}}
</option>
#endisset
#endforeach
#endif
</select>
I think the answer you have accepted here is not 100% right. You have multiple data in $adminUserWarehouseSelectedData coming from controllers. As you have used multiple select here I assumed that.
So this is the proper way to do it.
<select multiple="multiple" name="warehouseId[]" id="warehouse" class="form-control" style="width:100%;">
#if($warehouseData)
#foreach ($warehouseData as $key => $warehouse)
<option value="{{$warehouse->id}}" >{{$warehouse->name}} #foreach($adminUserWarehouseSelectedData as $data) $data->id #endforeach {{$warehouse->name}}</option>
#endforeach</option>
#endforeach
#endif
</select>
I know that you are not using this code but for other users correct answer is the most preferable answer.
Related
I try to get the selected option for the Team select on my form to further use it in the same form to limit the available positions in the selected team.
I managed to limit each team, so that it has a maximum of 9 players.
The idea is to have +-2 players on each position( 2 forward, 2 center, 2 back, 3 substitutes) for each team, if the spots are full the 'position' (ex. forward) option doesn't show up for the selected team.
Might be helpful : I am using foreign keys : (player -> one position_id, team -> many players)
My positions table is consisted of id, name and max and it is passed as $positions
I am trying to figure out a way, i am new to the subject so if you guys can help me, would be a great.
Or tell me if it isn't possible.
Thanks in advance.
<form action="/player" method="POST" enctype="multipart/form-data" class="form-control">
#csrf
...
<div class="mb-3">
<select name="team_id">
<option selected>Select Team</option>
#foreach ($teams as $team)
#if ($team->players->count() < 9)
<option value="{{ $team->id }}">{{ $team->name }}</option>
#endif
#endforeach
</select>
</div>
<div class="mb-3">
<select name="position_id">
<option selected>Select Position</option>
#foreach ($teams as $team)
#if ($team->id == ???)
#foreach ($positions as $position)
#if ($position->count() > $position->max)
<option value="{{ $position->id }}" disabled>{{ $position->name }}</option>
#else
<option value="{{ $position->id }}">{{ $position->name }}</option>
#endif
#endforeach
#endif
#endforeach
</select>
</div>
...
<button type="submit" class="btn btn-primary">submit</button>
</form>
```
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 got the code snippet,
<div class="form-group">
<label for="category-id-field">Category</label>
<select class="form-control" name="category_id" id="category-id-field">
<option value="" hidden disabled {{ $topic->id ? '' : 'selected' }}>Choose your category</option>
#foreach ($categories as $value)
#php
$selected = $topic->category_id === $value->id ? 'selected' : '';
#endphp
<option value="{{ $value->id }}" {{ $selected }}>{{ $value->name }}</option>
#endforeach
</select>
</div>
And it shows like below, as you can see $value->name works but $value->id just shows up zero.
<div class="form-group"><label for="category-id-field">Category</label>
<select name="category_id" id="category-id-field" class="form-control">
<option value="" selected="selected">Choose your category</option>
<option value="0">statement</option>
<option value="0">life</option>
<option value="0">work</option>
<option value="0">study</option>
</select>
</div>
Versions:
laravel/framework: "^7.0"
Platform: macos catalina
mysql: 8.0.19
Some more background:
Things I did, the id in the categories table used to be integer, I changed it to VARCHAR(30) and added it as the foreign key to table topics->category_id.
If anyone happens to have some experience on this one, please point out the error. Many thanks.
I am using Adminlte 3 Multiple (.select2-purple) in laravel 6, and I got a error like this
I don't know why searching... is showing
Here's my code:
VIEW
<div class="col-md">
<div class="form-group">
<label>Technician</label>
<div class="select2-purple">
<select class="select2" multiple="multiple" data-placeholder="Select a Technician" data-
dropdown-css-class="select2-purple" style="width: 100%;">
#foreach($stations as $station)
<option id="{{ $station->id }}">{{ $station->name }}</option>
#endforeach
</select>
</div>
</div>
</div>
<script>
$('.select2').select2()
</script>
Controller
public function totaltestperform(){
$stations = \DB::table('stations')
->select('id','station')
->orderby('id')
->get();
return view("analytic/total_test_perform",compact('stations'));
}
Here's the output of station variable when I use dd($stations);
Wrong: <option **id="{{ $station->id }}"**>{{ $station->name }}</option>
Use: <option value="{{ $station->id }}">{{ $station->name }}</option>
I have two Model(Category,Brand) are relate with one to many relation(hasMany) in laravel, Like category hasMany brands. now i want to display brand data in one select tag and category model data in another select tag.
Here is my Controller code
$category=Category::with('brands')->get();
return view('product.add')->with('category',$category);
Here is my view blade.
<div class="form-group">
<select name="category_id" class="input">
<option value="">Select Main Category</option>
#foreach($category as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="brand_id" class="input">
#foreach($brands->brands as $brand)
<option value="{{$brand->id}}">{{$brand->name}}</option>
#endforeach
</select>
</div>
its not working
You can do something simple like this:
<div class="form-group">
<select name="category_id" class="input">
<option value="">Select Main Category</option>
#foreach($category as $cat)
<option value="{{$cat>id}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="brand_id" class="input">
#foreach($category as $cat)
#foreach($cat->brands as $brand)
<option value="{{$brand->id}}" category="{{$cat->id}}">{{$brand->name}}</option>
#endforeach
#endforeach
</select>
</div>
and you need to show only the brands for the selected option using javascript(jquery)
$('[name="category_id"]').on('change',function() {
if($(this).val() != '') {
$('[name="brand_id"] option').hide();
$('[name="brand_id"] option[category="'+$(this).val()+'"]').show();
} else {
$('[name="brand_id"] option').show();
}
});