LoginSignup
12
14

More than 5 years have passed since last update.

Laravel4のフォームでDBから持ってきたデータをSELECTに並べる

Posted at

Controllerでこんなかんじで一覧を取って渡す。

$categories = Category::all()->lists('name','id');
return View::make('index',[
                           'categories' => $categories,
                           ]);

Eloquentのlistsっていうメソッドで上記のように書くと以下のように連想配列が取れる。

[
  "1" => "Category1",
  "2" => "Category2",
]

で、以下のように渡してやる。

{{ Form::select('category_id', $categories, null, ['class' => 'some-class']); }}

Form::selectは第3引数はデフォルトで選択されている項目、第4引数はHTMLの要素に指定する属性。

12
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
14