我在使用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) {
}
}
}
Collection
转换为数组。 - Bailey Parkervar_dump(Category:all());
。我猜它会返回一个数组。 - Bailey Parker