3

選択ボックスでいくつかの問題に直面しています。使用可能なすべてのカテゴリを

私のコントローラーでは、このスニップを使用しています:

 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) {
}
}
}
4

4 に答える 4

0

イスラエルオルトゥーニョが提案したアプローチが好きです

デフォルトで空の「リストから選択」オプションで選択が開始されるように、小さな変更のみを追加します。

$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>
...
于 2015-01-07T13:10:25.940 に答える