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

推荐答案

如果从控制器传递 Select 值:

$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>

Laravel相关问答推荐

到查询构建器的MySQL查询

如何解决此 Backblaze B2 S3 兼容 API 错误?

V-icon 在 Vuetify 3 中不显示图标

laravel Eloquent 模型更新事件未触发

Laravel 5 Eloquent 范围连接并 Select 特定列

Laravel 不验证是否不需要字段

如何在 Laravel 4 中使用没有 id 的 Eloquent 更新数据

如何在 laravel 中使用 str_replace

laravel url 验证变音符号

为什么人们将 .env 放入 gitignore?

Laravel 表 * 没有名为 * 的列

artisan 迁移错误找不到类'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver'

未找到 PHP 5.4 和 Laravel 类Memcached

在 Laravel 中无效后防止重定向到主页

Laravel lockforupdate(悲观锁)

SQLSTATE [01000]:警告:1265 列的数据被截断(truncated)

何时在 Laravel 中使用 Repository vs Service vs Trait?

Laravel - 动态创建表(无需迁移)

Eloquent 模型不更新 updated_at 时间戳

如何在 Laravel 5.3 中获取当前的语言环境