laravel - 如何使用 Laravel 在下拉列表中显示数据库中的选定值?
问题描述
我想在页面加载时将数据库中的选定值显示到下拉列表中。
我的控制器索引功能是:
public function index()
{
$country_data =DB::table('country')->select('country_id','country_name')->get();
$profile_data= DB::table('profiles')->select('*')->where('id',$user_id)->first();
return view('profile_update',compact('profile_data','country_data'));
}
数据库中高度的列名是:Height
我在 profile_update.blade.php 中的下拉菜单是
<select class="select4" name="country" id="country">
<option value="">Please Select</option>
@foreach($country_data as $country)
<option value="{{$country->country_id}}" {{$country_data->country == $country->country_id ? 'selected' : ''}}>{{$country->country_name}}</option>
@endforeach</select>
解决方案
这是我如何执行此操作的示例:
<select class="js-states browser-default select2" name="shopping_id" required id="shopping_id">
<option value="option_select" disabled selected>Shoppings</option>
@foreach($shoppings as $shopping)
<option value="{{ $shopping->id }}" {{$company->shopping_id == $shopping->id ? 'selected' : ''}}>{{ $shopping->fantasyname}}</option>
@endforeach
</select>
推荐阅读
- asp.net-core-mvc - 用户首次登录显示错误 Null Reference 异常
- javascript - 需要,js不同数量的参数
- c - 32 位比较比 64 位比较快吗?
- c# - 正则表达式地址模式
- amazon-web-services - 第二次运行 AWS Rekognition 人脸搜索
- c# - 如何解决 ASP.NET 中的多个端点匹配问题
- php - 如何使用 foreach 循环获取所有内容,除了那些在简单的 html dom.php 中具有某些类的内容
- performance - grep 仅在特定小时内生成的文件
- ruby-on-rails - Rails 自定义模型验证器取决于 check_box 值
- azure-iot-edge - IoT Edge 上的自定义 DPS 注册负载