Laravel将数组放入selectbox

时间:2013-06-18 07:21:47

标签: php laravel 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) {
}
}
}

4 个答案:

答案 0 :(得分:9)

以这种方式使用:

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

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

现在在视图中:

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

修改:在文档中找到Query builder # Select查看此内容

答案 1 :(得分: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);

答案 2 :(得分: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));

我希望这可以提供一些帮助。

答案 3 :(得分:0)

我喜欢Israel Ortuño

建议的方法

我只会添加一个小修改,以便选择以空&#34开始;默认情况下从List" 选项中选择。

$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>
...