【问题标题】:How to show selected value from database in dropdown using Laravel?如何使用 Laravel 在下拉列表中显示数据库中的选定值?
【发布时间】:2018-11-30 20:58:30
【问题描述】:

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

我的控制器索引函数是:

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>

【问题讨论】:

  • 类似于&lt;option value="5 Feet" @if($profile_data['Height'] == '5 Feet') selected @endif &gt;5 Feet &lt;/option&gt; 的内容并重复其他选项。

标签: laravel select dropdown laravel-5.5 selectedvalue


【解决方案1】:

好吧,如果有人还在寻找这个,这里是从数据库到刀片视图中选择下拉项目的简单方法

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

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

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

如果您不想从数据库中选择所有用户,您可以在pluck 条件之前使用where 条件作为

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

适用于 Laravel 8 和 Laravel6

【讨论】:

    【解决方案2】:

    为了完全理解它,你需要 laravel (MVC) 的基础知识, 假设,你有控制器。就我而言,我为下拉值制作了一个单独的表格。

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


    注意: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>
    

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


    另一种方法 - 采纳想法

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

    以上代码的输出


    【讨论】:

      【解决方案3】:

      @Sarita Sharma 显示错误。也许然后有人可以帮助您解决此问题。在控制器的集合中显示数据 - 使用 dd 例如

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

      您想在视图中使用来自 profile_data 的适当高度值显示国家/地区吗?

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • @foreach($country_data as $country) @endforeach ,这会产生错误
        猜你喜欢
        • 1970-01-01
        • 2021-07-13
        • 2014-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-14
        • 2020-09-14
        相关资源
        最近更新 更多