3

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

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

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

4 回答 4

9

以这种方式使用它:

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

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

现在在视图中:

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

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

于 2013-06-18T08:19:22.773 回答
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);
于 2013-06-18T07:58:40.000 回答
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));

我希望这能有所帮助。

于 2013-06-18T07:55:42.730 回答
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>
...
于 2015-01-07T13:10:25.940 回答