如何使用 Laravel 在下拉列表中显示从数据库中选择的值?

How to show selected value from database in dropdown using 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'));
}

数据库中身高的列名是:身高

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

@Sarita Sharma 显示错误。也许那时有人会帮助您解决这个问题。在控制器中显示集合中的数据 - 使用 dd 例如

dd($country_data);
dd($profile_data);

是否要在视图中使用来自 profile_data 的适当高度值显示国家/地区?

Ps。如何放置代码,请使用格式化代码-将代码放在``中并在值名称中使用camelCase(保持良好的PSR-1习惯)-更容易阅读代码。

为了完全理解它,您需要 laravel (MVC) 的基础知识, 假设你有控制器。在我的例子中,我为下拉值做了一个单独的 table。

我的方法 --- 单独 table 用于下拉值,您可以尝试不同的方法,但我的解释主要集中在概念上。


注意:PersonInfo 是模型,sampleType 是我下拉值的模型,不要忘记为控制器创建路由。

ExampleController{

public funtion getValues($id){

/*
I am fetching information of a single row from the database by id and 
then passing it to the view. The $id to the function came from the 
request by your view/form. 
I also fetched the values of drop down from the separate table 
of database and passed it to the exampleView.
*/

$selectedValue=PersonInfo::findOrFail($id);
$sampleType=SampletypePicker::all(); //model for fetching all values of drop down

return view('exampleView',compact('selectedValue','sampleType));

   }
}

因此,在上面的控制器中,我获取了所有值并将其传递给 ExampleView。现在在您的视图文件中,您必须以同样的方式工作。

exampleView.blade.php 添加以下代码


    <?php $options=$selectedValue->sample_type ?> //note take the value from the database which stored for the individual record and match it with the selected one.
    <select class="form-control" name="test">
              <option>Select Test Type</option>
    @foreach ($sampleType as $value)
             <option value="{{ $value->sample_type }}" {{ ( $value->sample_type == $options) ? 'selected' : '' }}>
                {{ $value->sample_type }}
              </option>
     @endforeach
    </select>

当您想向下拉列表添加动态值时,这是最好的方法之一。我的意思是,如果您想向 select 标签选项添加更多值,那么这是最好的方法。


另一种方法 - 接受想法

      <?php $months = array("Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");   ?>

        <?php $options=$patient_data->month ?>

                @if($patient_data->month)
                <select id="expiry_month" name="month" class="form-control-sm">
@foreach($months as $month)
                     <option value="{{$month}}" {{($month==$options)? 'selected':'' }}>{{$month}}</option>
@endforeach
               </select>

            @endif

                <span class="select-icon"><i class="zmdi zmdi-chevron-down"></i></span>
            </div>

以上代码的输出


好吧,如果有人还在寻找这个,这里是从数据库下拉 select 项目到 blade 视图

的简单方法
$users = Users::pluck('name', 'id');
$selectedID = 2;
return view('users.edit', compact('id', 'users'));

在渲染视图之前在控制器函数中添加这个 在 blade 视图中,只需将此变量用作 foreach 循环 as

<select name="user_id" class="form-control" id="exampleFormControlSelect1">
    @foreach ($users as $key => $value)
    <option value="{{ $key }}" {{ ($key == $selectedID) ? 'selected' : '' }}> 
      {{ $value }} 
    </option>
    @endforeach
</select>

如果您不想 select 来自数据库的所有用户,您可以在 pluck 条件之前使用 where 条件作为

$users = Users::where('status', 1)->pluck('name', 'id');

适用于 Laravel 8 和 Laravel6