laravel 使用数据库表中的值生成下拉列表输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14221444/
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 07:28:42  来源:igfitidea点击:

Generate drop-down list input with values from DB table

phplaravel

提问by NightMICU

I am trying to generate a drop-down list with values from a MySQL table using Laravel. The table is simple, two columns - idand category.

我正在尝试使用 Laravel 从 MySQL 表中生成一个包含值的下拉列表。该表很简单,两列 -idcategory

The following will retrieve all of the records (categories) but returns an object rather than an array, which is what I need for the drop-down code -

以下将检索所有记录(类别),但返回一个对象而不是数组,这是下拉代码所需要的 -

$categories = Category::all();

The code for the drop-down is:

下拉框的代码是:

{{ Form::select('category', $categories, $post->category_id) }}

Ideas?

想法?

UPDATE

更新

bgallagh3r suggested using a foreach loop to convert each category to an array. Their code got me close but generated a bunch of funky nested optgrouptags. I was able to get it down to just one optgroupbut that is one too many..

bgallagh3r 建议使用 foreach 循环将每个类别转换为数组。他们的代码让我很接近但生成了一堆时髦的嵌套optgroup标签。我能够把它归结为一个,optgroup但那太多了。

$categories = Category::all();
foreach ($categories as $cat)
{
    $category = $cat->to_array();
    $id = $category['id'];
    $value = $category['category'];
    $cats[] = array($id => $value);
}

And then, in the form:

然后,在表格中:

{{ Form::select('categories', $categories)}}

I end up with this HTML:

我最终得到了这个 HTML:

    <select name="categories">
    <optgroup label="0">
    <option value="1">Department News</option>
    </optgroup>
    <optgroup label="1">
    <option value="2">General</option>
    </optgroup>
   ...
    </select>

回答by Barryvdh

You can try the 'lists' function:

您可以尝试“列表”功能:

$categories = Category::lists('category', 'id');

$categories = Category::lists('category', 'id');

(Only for Laravel 3) Or even leave out the 'id' parameter, as it will default to the Model key, see http://laravel.com/api/source-class-Laravel.Database.Query.html#596

(仅适用于 Laravel 3)或者甚至省略 'id' 参数,因为它将默认为 Model 键,请参阅http://laravel.com/api/source-class-Laravel.Database.Query.html#596

$categories = Category::lists('category');

$categories = Category::lists('category');

(For Laravel 4, you do need the second param, see http://laravel.com/api/source-class-Illuminate.Database.Query.Builder.html#1034-1063)

(对于 Laravel 4,您确实需要第二个参数,请参阅http://laravel.com/api/source-class-Illuminate.Database.Query.Builder.html#1034-1063

回答by bgallagh3r

Depending on if you are using Eloquent for your models Category will return an array of objects, you need to convert the objects to arrays before passing them to the Form class.

根据您是否为模型使用 Eloquent,Category 将返回一个对象数组,您需要将对象转换为数组,然后再将它们传递给 Form 类。

foreach (Category::all() as $cat)
{
    $categories[] = array($cat->id => $cat->category);
}
{{ Form::select('categories', $categories)}}

回答by Set Kyar Wa Lar

In Laravel 4, you can easily try like the following

在 Laravel 4 中,你可以很容易地尝试如下

//Query category and pass it  to the view that you want to show your category
$category = Category::lists('category_name', 'category_id');

In your view just add {{ Form::select('category', $category) }}. The result will be what you want, like the following

在您看来,只需添加{{ Form::select('category', $category) }}. 结果将是您想要的,如下所示

<select name="category">
        <option value="category_id">category_name</option>
</select>

回答by Octavio Fuenzalida

For Laravel 5.3.x

对于 Laravel 5.3.x

In Controller

在控制器中

$categories = Category::pluck('name', 'id');

In View

在视图中

{{ Form::select('categories', $categories)}}

回答by HymanpotK

How about this ?

这个怎么样 ?

foreach (Category::all() as $cat)
{
    $categories[$cat->id] = $cat->category;
}
{{ Form::select('categories', $categories)}}

回答by James

Just tweeking bgallagh3r's code a little the following will work:

只需稍微调整 bgallagh3r 的代码,以下内容就可以工作:

foreach (Category::all() as $cat)
{
    $categories[$cat->id] = $cat->category);
}
{{ Form::select('categories', $categories)}}

...although I prefer (In L4):

...虽然我更喜欢(在 L4 中):

foreach (Category::select('id', 'category')->orderBy('id','asc')->get() as $cat)
{
  $categories[$cat->id] = $cat->category;
}
{{ Form::select('categories', $categories)}}

回答by Khan Muhammad

for laravel 5.2

用于 Laravel 5.2

in controller

在控制器中

$mechanics = User::where('role_id','3')->lists('name', 'id');

in view

鉴于

{{   Form::select('mechanic_id', 
    [''=>'please select'] + $mechanics->toArray(), $order->mechanic_id , 
     array('id' =>'material-select2','class' => 'form-control'))  }}