【问题标题】:Populate Dropdown using database query results使用数据库查询结果填充下拉列表
【发布时间】:2015-01-21 21:41:18
【问题描述】:

首先,我对 Laravel 非常熟悉。因此,如果我使用任何错误的术语或没有掌握一些对你们所有人来说应该显而易见的东西,请考虑到这一点。 我正在尝试使用数据库查询的结果填充下拉列表。

下拉条目模型:

  Class Dropdown {

    public static function getFilters() {
          $filter = DB::select(DB::raw('SELECT DISTINCT filter AS filt
                                    FROM filter.database'));
    return $filter;
    }

  }

控制器:

  class FilterController extends BaseController {
       public function getSql_Test() {
            $dropdown = Dropdown::getFilters();
            return View::make('Dropdown.show', ['Dropdown' =>$dropdown]);
       }
  }

我的控制器现在完成的是在页面上显示查询结果(我想填充下拉列表)。有人可以帮助我如何将这些条目传递到此处的选择标签上并将其显示在我的视图上吗?

【问题讨论】:

    标签: php html mysql laravel-4


    【解决方案1】:

    这很容易做到,而且很有趣(因为它很容易)!

    $filter = DB::table('filter.database')->select(DB::raw('DISTINCT(filter) AS filt'))
        ->lists('filt');
    

    这将为您提供一个数组,其中键是整数,值是您的过滤器。

    $filter = DB::table('filter.database')->select(DB::raw('DISTINCT(filter) AS filt'))
        ->lists('filt', 'filt');
    

    这将为您提供一个数组,其中键为 filt,值为 filt。

    您可以通过使用如下变量扩展视图来将其推送到视图中:

    return View::make('Dropdown.show')->with('dropdown', $dropdown);
    

    现在您可以进行选择,然后将值推入数组中:

    echo Form::select('size', $dropdown);
    

    更多信息可以在这里找到:

    http://laravel.com/docs/4.2/queries#selects

    作为提示,请继续阅读 laravel 文档。与他们一起工作是一种享受。

    【讨论】:

    • 非常感谢您的投入。但是,我收到错误消息 Call to a member function lists() on a non-object。有什么建议吗?
    • 我认为这与使用雄辩的方式有关。我改变了一些东西。应该工作
    • 你是对的。这是我编写查询的方式。现在可以了。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 2014-08-27
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    相关资源
    最近更新 更多