将数组放入 Laravel 的下拉框中

3

我在使用selectbox时遇到了一些问题,我需要将所有可用的类别放入其中。

在我的控制器中,我使用了以下代码片段:

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

我认为我正在尝试使用以下代码将所有类别放入选择框中:

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

我可以制作这个,但是那样做不起作用,因为Form::select必须作为一个数组?

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

怎么办?

我已经制作了这个功能,它可以正常工作,但它看起来太丑了,而且不够用户友好。有什么建议吗?

  $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) {
}
}
}

你正在使用Laravel 3还是Laravel 4?你只需要用一个标签即可。 - Bailey Parker
在 Laravel 4 中,您应该能够使用 Category::all()->all() 将 Collection 转换为数组。 - Bailey Parker
调用非对象的成员函数all()。 - kim larsen
执行 var_dump(Category:all());。我猜它会返回一个数组。 - Bailey Parker
4个回答

9

使用方法如下:

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

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

现在在视图中:

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

编辑:在文档中找到了查询构建器#选择,看一下这个。


嘿,你确定 Category::lists('name', 'id'); 是有效的吗?我不清楚查询构建器或模型中的 lists 函数。 - Shreyansh Panchal
lists function is deprecated & replaced by pluck() as of Laravel > 5.2Now you can do Category::pluck('name', 'id'); - Shreyansh Panchal

4
你需要做的是给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);

说“pluck”不存在。 - kim larsen

2
您需要做的是在控制器函数中使用视图放置而不是使用with()函数。
$categories = Category::all();

接下来您需要正确重构数组:

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

现在在View::make()中

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

我希望这能有所帮助。


不起作用。如果有多个类别,则在<option..>中应该显示名称而不是ID,即:<select name="category"><option value="id">2</option><option value="name">Bondage</option></select>中。 - kim larsen
1
抱歉,金姆,我的错误。我在foreach循环中进行了更改,请在您的脚本中进行相应更改,希望这样就可以解决问题了。 - saran banerjee

0

我喜欢Israel Ortuño提出的方法。

我只会添加一个小修改,使得选择框默认从空的"从列表中选择"选项开始。

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

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

现在下拉菜单看起来像这样:
<option value="0">Choose from the list</option>
<option value="{whatever-the-category-id}">Category 1</option>
...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接