Old value in multiple select option in laravel bla

2020-07-03 08:33发布

Here is my select option

<select name="recomemded_food[]" value="" style="width:560px;" multiple class="chosen-select" >
<option value="American Black Bear">American Black Bear</option>
<option value="Asiatic Black Bear">Asiatic Black Bear</option>
<option value="Brown Bear">Brown Bear</option>
<option value="Giant Panda">Giant Panda</option>
</select>

And below is my code trying to use the foreach loop to get the array value. but I am receiving the following error:

@foreach (explode(',',old('recomemded_food')) as $recomemded_food) 
{{$recomemded_food}}
@endforeach

Error Message : explode() expects parameter 2 to be string

7条回答
女痞
2楼-- · 2020-07-03 08:50

If you pass the select values from Controller:

$recommended_foods = ["American Black Bear",
                       "Asiatic Black Bear",
                       "Brown Bear",
                       "Giant Panda"];

and In the view:

<select required="required" class="form-control" name="recommended_food">
    @foreach ($recommended_foods as $key => $food)
        <option value="{{ $food}}" {{ (old("recommended_food") == $food ? "selected":"") }}>{{ $food }}</option>
    @endforeach
</select>
查看更多
冷血范
3楼-- · 2020-07-03 08:50

I was parsing JSON data, and was faced with repopulating the multi-select as an array, and only wanted one table, and wasn't using keys since this array was being passed back from the show method in the controller, so had to supply the values directly on the template, in my case as a crud resource on the edit.blade.php template (also on the create template).

This worked for me, and was the cleanest I could get it on the view.

<select id="recommended_food"   multiple="multiple" size=3 style='height: 100%;' name="recommended_food[]">
    @if ($food->recomemded_food)
      {{ ($val = 'Fries')
        && $chosen = in_array($val, $food->recommended_food)
        ? 'selected':null}}
      <option value="{{$val}}" {{$chosen}}>{{$val}}</option>
      {{ ($val = 'Hot Dogs')
        && $chosen = in_array($val, $food->recommended_food)
        ? 'selected':null}}
      <option value="{{$val}}" {{$chosen}}>{{$val}}</option>
      {{ ($val = 'Hamburgers')
        && $chosen = in_array($val, $food->recommended_food)
        ? 'selected':null}}
      <option value="{{$val}}" {{$chosen}}>{{$val}}</option>
    @endif
  </select>
查看更多
混吃等死
4楼-- · 2020-07-03 08:52

For me Jilson Thomas's answer did not work for multiple select so I changed it to:

{{ in_array($food, old("recommended_food")) ? "selected":"") }}
查看更多
Viruses.
5楼-- · 2020-07-03 08:55

I think the best answer to this question is this below

@if(count($categories) > 0)
  @foreach($categories as $category)
      <option value="{{ $category->id }}" {{ (old('lawyer_fields') == null ? '' : in_array($category->id ,old('lawyer_fields')) ? "selected":"") }}>
                                            {{ $category->name }}
      </option>
  @endforeach
  @else
       <option disabled>بدون انتخاب</option>
@endif
查看更多
Viruses.
6楼-- · 2020-07-03 09:03

After Playing around a bit I came up with this and it seems to work just splendidly

<select name="options[]" id="options" class="form-control" multiple>
    @foreach($settings->includes->get('optionList') as $option)
        <option value="{{ $option->id }}" {{ (collect(old('options'))->contains($option->id)) ? 'selected':'' }}>{{ $option->name }}</option>
    @endforeach
</select>

I may be 100% wrong in leveraging the collect function but it works fine on many of my tests. I want to say thank you to MPS as it was your example that really made me try this as when there is no data in "recommended_food" you will pull an error because in_array requires an array and will not work well with null so I then came up with the idea of doing something like this.

@if (old("options")){{ (in_array($option->id, old("options")) ? "selected":"") }}@endif

inline but man that looks ugly to me so long story short I am using the following instead

{{ (collect(old('options'))->contains($option->id)) ? 'selected':'' }}

Hope this helps others!!

查看更多
该账号已被封号
7楼-- · 2020-07-03 09:04

The question was asked for multiple chosen.

Assuming you have a field called designation and you need to choose multiple designation for adding 1 record and the field name is forWhom

During Add

You need to add this {{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }} peace of code to your option tag

which will look like

<select id="forWhom" name="forWhom[]" multiple class="form-control chosen">
    <option value="">--- Select ---</option>
    @foreach ($desgInfo as $key => $value)
        <option value="{{ $key }}" 
           {{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }}  />
           {{ $value }}
        </option>
    @endforeach
</select>

During Edit

You need to add

{{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }} 

{{ (in_array($key,$info->forWhom)) ? 'selected' : ''}}

which will look like

<select id="forWhom" name="forWhom[]" multiple class="form-control chosen">
        <option value="">--- Select ---</option>
        @foreach ($desgInfo as $key => $value)
            <option value="{{ $key }}" 
               {{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }} 
               {{ (in_array($key,$info->forWhom)) ? 'selected' : ''}}  
               />
               {{ $value }}
            </option>
        @endforeach
    </select>

Select all selected id's in a multiselect dropdown in laravel 5.4 with harvest chosen

Thanks

查看更多
登录 后发表回答