2013-06-18 50 views
3

我现在面临的一些问题,我的选择框,在这里我会把所有可用的类别到Laravel把数组选择框

在我的控制,我使用这个剪断:

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

在我看来,我我试图把所有类别到选择框与此:

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

我可以做这个,但是这是行不通的,因为格式是::选择必须是一个数组?

@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) { 
} 
} 
} 
+0

您是否使用Laravel 3或4 Laravel?你只需要用一个标签来标记。 –

+0

我正在使用laravel 3 –

+0

在laravel 4中,您应该可以使用Category :: all() - > all()将'Collection'转换为数组。 –

回答

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); 
+0

说“拔”不存在。 –

+0

@ kimlarsen查看编辑 –

2

你需要做的是什么,而不是使用与把控制器功能内视图的使用()函数。

$categories = Category::all(); 

此之后,你需要正确地重建阵列:

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

现在在View ::使()

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

我希望这能有一定的帮助。

+0

不起作用。如果多于一个类别,它将显示ID而不是名称,其名称应该在

+1

对不起kim我的错误我在foreach循环中做了一个修改,请在脚本中进行修改,我希望应该这样做。 –

9

使用这种方式:

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

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

现在:

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

编辑:在文档Query builder # Select发现看一看这个

+0

还没有测试过,但是它的工作原理是光滑的语法! –

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