Laravel 将数组放入选择框

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

Laravel put array into selectbox

phplaravellaravel-3

提问by kim larsen

I am facing some problems with my selectbox, where i will put all available categories into the

我的选择框遇到了一些问题,我会将所有可用的类别放入

In my controller i am using this snip:

在我的控制器中,我正在使用这个片段:

 return View::make("stories.add")
        ->with("title","Indsend novelle")
        ->with("categories", Category::all());

In my view i am trying to put all categories into the selectbox with this:

在我看来,我正在尝试将所有类别放入选择框中:

 {{Form::select("category", $categories)}}

I could make this, but that won't work, because Form::select has to be as an array?

我可以做到这一点,但这行不通,因为 Form::select 必须是一个数组?

@foreach ( $categories as $category )
    {{$category->name}}
@endforeach

What to do?

该怎么办?

I have made this and it works, but it looks too ugly and not user-friendly, any suggestions?

我已经做了这个并且它有效,但它看起来太丑陋而且不用户友好,有什么建议吗?

  $test = Category::all(); $myArray = array();
    foreach ( $test as $o):
          $myArray[] = $o->name;
    endforeach;

    return View::make("stories.add")
        ->with("title","Indsend novelle")
        ->with("categories", $myArray);

var_dump:

变量转储:

    array(2) {
      [0]=>
      object(Category)#36 (5) {
        ["attributes"]=>
array(4) {
  ["id"]=>
  string(1) "1"
  ["name"]=>
  string(12) "Alderforskel"
  ["created_at"]=>
  string(19) "0000-00-00 00:00:00"
  ["updated_at"]=>
  string(19) "0000-00-00 00:00:00"
}
["original"]=>
array(4) {
  ["id"]=>
  string(1) "1"
  ["name"]=>
  string(12) "Alderforskel"
  ["created_at"]=>
  string(19) "0000-00-00 00:00:00"
  ["updated_at"]=>
  string(19) "0000-00-00 00:00:00"
}
["relationships"]=>
array(0) {
}
["exists"]=>
bool(true)
["includes"]=>
array(0) {
}
}
       [1]=>
   object(Category)#39 (5) {
  ["attributes"]=>
  array(4) {
  ["id"]=>
  string(1) "2"
  ["name"]=>
  string(7) "Bondage"
  ["created_at"]=>
  string(19) "0000-00-00 00:00:00"
  ["updated_at"]=>
  string(19) "0000-00-00 00:00:00"
}
["original"]=>
 array(4) {
  ["id"]=>
  string(1) "2"
  ["name"]=>
  string(7) "Bondage"
  ["created_at"]=>
  string(19) "0000-00-00 00:00:00"
  ["updated_at"]=>
  string(19) "0000-00-00 00:00:00"
}
["relationships"]=>
array(0) {
}
["exists"]=>
bool(true)
["includes"]=>
array(0) {
}
}
}

回答by Israel Ortu?o

Use it this way:

以这种方式使用它:

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

return View::make('....', compact('categories'));

And now in the view:

现在在视图中:

{{ Form::select('selectName', $categories, null); }}

Edit: Found in the docs Query builder # SelectHave a look to this

编辑:在文档查询生成器中找到# 选择看看这个

回答by Bailey Parker

What you need to do is give Form::select()an array of category names and their ids. If you iterate over the categories, you can aggregate these and then pass them to Form::select().

您需要做的是提供Form::select()一组类别名称及其 ID。如果您遍历类别,则可以聚合这些类别,然后将它们传递给Form::select().

$categories = Categories::all();
$selectCategories = array();

foreach($categories as $category) {
    $selectedCategories[$category->id] = $category->name;
}

return View::make("stories.add")
        ->with("title","Indsend novelle")
        ->with("categories", $selectCategories);

回答by saran banerjee

What you need to do is instead of using the with() function with the view put inside the controller function.

您需要做的是,不要将 with() 函数与放置在控制器函数中的视图一起使用。

$categories = Category::all();

After this you need properly reconstruct the array:

在此之后,您需要正确重建数组:

$category = array();
foreach($categories as $cat)
{
  $category[]['id'] = $cat->attributes['id'];
  $category[]['name'] = $cat->attributes['name'];
}

now in the View::make()

现在在 View::make()

return View::make("stories.add",array('title'=> "Indsend novelle","categories", $category));

I hope this can be of some help.

我希望这能有所帮助。

回答by Nat Naydenova

I like the approach suggested by Israel Ortu?o

我喜欢Israel Ortu?o建议的方法

I would only add a small modification, so that the select starts with an empty "Choose from the List"option by default.

我只会添加一个小的修改,以便默认情况下选择以空的“从列表中选择”选项开始。

$categories = array(0=>'Choose from the list') + Category::lists('name', 'id');

return View::make('....', compact('categories'));


Now the dropdown looks like this:


现在下拉菜单如下所示:

<option value="0">Choose from the list</option>
<option value="{whatever-the-category-id}">Category 1</option>
...