【发布时间】:2013-06-14 07:31:15
【问题描述】:
我的选择框遇到了一些问题,我会将所有可用类别放入其中
在我的控制器中,我正在使用这个片段:
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?你应该只需要用一个来标记它。
-
在 laravel 4 中,您应该可以使用 Category::all()->all() 将
Collection转换为数组。 -
在非对象上调用成员函数 all()
-
做一个
var_dump(Category:all());。我怀疑它返回一个数组。