Reputation: 102
I would like to display default values inside of my select using bootstrap-select, when I'm editing it. How can I achieve this?
bootstrap-select sends data in array, but could I pass aray into it? How could I set default value of select?
Data that goes as default
@foreach ($product->categories as $origin)
{{$origin->category->id}} // <- This one needs to be sent
{{$origin->category->name}} // <- This one needs to be displayed
@endforeach
My select:
<select name="category_id[]" values="" class="form-control selectpicker" multiple data-live-search="true">
@foreach($categories as $category)
<option value="{{$category->id}}">{{ $category->name }}</option>
@endforeach
</select>
I tried doing it using an array, so I would just put array into values:
@foreach ($product->categories as $index=>$origin)
{{$passTo[$index] = $origin->category->id}}
{{$origin->category->name}}
@endforeach
But there are two problems:
It prints the value while creating array (since {{}} is like echo in blade)
It doesnt work:
<select name="category_id[]" values="{{$passTo}}" class="form-control selectpicker" multiple data-live-search="true">
My second idea was to pass them as selected options but I dont know how to avoid duplicating records between 2 foreaches:
@foreach($product->categories as $origin)
@foreach($categories as $category)
@if ($category->id == $origin->category->id)
<option value="{{$origin->category->id}}" selected>
{{$origin->category->name}}
</option>
@else
<option value="{{$category->id}}">
{{ $category->name }}
</option>
@endif
@endforeach
@endforeach
Second solution might be the easiest one, but still I`m not sure how to complete it :c
Screen of what I would like to achieve(just in case)
Upvotes: 0
Views: 174
Reputation: 102
It might not be the best solution. But it works :)
@php
$taken = array();
foreach ( $product->categories as $origin )
{
array_push($taken, $origin->category);
}
$all = array();
foreach ($categories as $category)
{
array_push($all, $category);
}
$rest = array();
$rest = array_diff($all, $taken);
@endphp
Upvotes: 0