【问题标题】:Laravel put array into selectboxLaravel 将数组放入选择框
【发布时间】:2013-06-14 07:31:15
【问题描述】:

我的选择框遇到了一些问题,我会将所有可用类别放入其中

在我的控制器中,我正在使用这个片段:

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

【问题讨论】:

  • 你使用的是 Laravel 3 还是 Laravel 4?你应该只需要用一个来标记它。
  • 在 laravel 4 中,您应该可以使用 Category::all()->all() 将 Collection 转换为数组。
  • 在非对象上调用成员函数 all()
  • 做一个var_dump(Category:all());。我怀疑它返回一个数组。

标签: php laravel laravel-3


【解决方案1】:

这样使用:

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

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

现在在视图中:

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

编辑:在文档中找到 Query builder # Select 看看这个

【讨论】:

  • 嘿,你确定Category::lists('name', 'id'); 这是有效的吗?我不知道查询构建器或模型上的 lists 函数。
  • lists 函数已弃用并被 pluck() 替换为 Laravel > 5.2 现在你可以做 Category::pluck('name', 'id');
【解决方案2】:

我喜欢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>
...

【讨论】:

    【解决方案3】:

    您需要做的是给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);
    

    【讨论】:

    • 说“pluck”不存在。
    【解决方案4】:

    您需要做的是,而不是使用 with() 函数将视图放在控制器函数中。

    $categories = Category::all();
    

    在此之后,您需要正确地重建数组:

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

    现在在视图中::make()

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

    我希望这能有所帮助。

    【讨论】:

    • 不起作用。如果超过 1 个类别,则显示 ID 而不是名称,名称应位于
    • 对不起,我的错误我在 foreach 循环中进行了更改,请在您的脚本中进行更改,我希望这样可以解决问题。
    猜你喜欢
    • 2015-05-16
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 2014-09-13
    相关资源
    最近更新 更多