Laravel 5.2 - 从数据库中填充选择选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35421804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:14:32  来源:igfitidea点击:

Laravel 5.2 - Populate select options from database

phplaravelselectoptionsdropdown

提问by Ajaxcbcb

Trying to populate the dropdownlist from database. but there having some complication. Any help? This is based on laravel framework.

试图从数据库填充下拉列表。但有一些复杂性。有什么帮助吗?这是基于laravel框架。

Part of the code

部分代码

$projects = DB::table('projects')->where('users_id','=',Auth()->User()->id)->get();

                        foreach($projects as $project){
                            echo '
                                    <option value="<?php $id = $project[id] ?>" name="parentProj">'
                                        .$projectName = $project[id].
                                    '</option>
                                  ';
                        }

Controller

控制器

public function index(){
    $projects = DB::table('projects')->lists('id','projectName');

    return view('pages.todo', ['projects'=> $projects);
}

回答by Jilson Thomas

In your view:

在您看来:

<select name="parentProj">
    @foreach($projects as $project)
     <option value="{{ $project->id }}">{{ $project->projectName}}</option>
    @endforeach
</select>

回答by vonec

Or you can use the Forms & HTML package from Laravel Collective: https://laravelcollective.com/docs/5.2/html#drop-down-lists

或者你可以使用 Laravel Collective 的 Forms & HTML 包:https://laravelcollective.com/docs/5.2/html#drop-down-lists

{{ Form::select('Projects', $projects)) }}