如何使用 Laravel 在下拉列表中显示数据库中的选定值? [英] How to show selected value from database in dropdown using Laravel?

查看:21
本文介绍了如何使用 Laravel 在下拉列表中显示数据库中的选定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在页面加载时将数据库中的选定值显示到下拉列表中.

I want to show selected value from database into dropdown list on page load.

我的控制器索引函数是:

My controller index function is :

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

Column name in database for height is :Height

我在 profile_update.blade.php 中的下拉列表是

My dropdown in profile_update.blade.php is

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

推荐答案

这是我如何执行此操作的示例:

This is a example of how I do this:

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

这篇关于如何使用 Laravel 在下拉列表中显示数据库中的选定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆