2013-06-18 127 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> 
...