Laravel put array into selectbox

不问归期 提交于 2019-12-07 15:38:10

问题


I am facing some problems with my selectbox, where i will put all available categories into the

In my controller i am using this snip:

 return View::make("stories.add")
        ->with("title","Indsend novelle")
        ->with("categories", Category::all());

In my view i am trying to put all categories into the selectbox with this:

 {{Form::select("category", $categories)}}

I could make this, but that won't work, because Form::select has to be as an array?

@foreach ( $categories as $category )
    {{$category->name}}
@endforeach

What to do?

I have made this and it works, but it looks too ugly and not user-friendly, any suggestions?

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

回答1:


What you need to do is give Form::select() an array of category names and their ids. If you iterate over the categories, you can aggregate these and then pass them to 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);



回答2:


Use it this way:

$categories = Category::lists('name', 'id');

return View::make('....', compact('categories'));

And now in the view:

{{ Form::select('selectName', $categories, null); }}

Edit: Found in the docs Query builder # Select Have a look to this




回答3:


What you need to do is instead of using the with() function with the view put inside the controller function.

$categories = Category::all();

After this you need properly reconstruct the array:

$category = array();
foreach($categories as $cat)
{
  $category[]['id'] = $cat->attributes['id'];
  $category[]['name'] = $cat->attributes['name'];
}

now in the View::make()

return View::make("stories.add",array('title'=> "Indsend novelle","categories", $category));

I hope this can be of some help.




回答4:


I like the approach suggested by Israel Ortuño

I would only add a small modification, so that the select starts with an empty "Choose from the List" option by default.

$categories = array(0=>'Choose from the list') + Category::lists('name', 'id');

return View::make('....', compact('categories'));


Now the dropdown looks like this:

<option value="0">Choose from the list</option>
<option value="{whatever-the-category-id}">Category 1</option>
...


来源:https://stackoverflow.com/questions/17162876/laravel-put-array-into-selectbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!