我现在面临的一些问题,我的选择框,在这里我会把所有可用的类别到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) {
}
}
}
您是否使用Laravel 3或4 Laravel?你只需要用一个标签来标记。 –
我正在使用laravel 3 –
在laravel 4中,您应该可以使用Category :: all() - > all()将'Collection'转换为数组。 –