【问题标题】:Best way to make selects in laravel?在 laravel 中进行选择的最佳方法?
【发布时间】:2013-02-08 13:49:12
【问题描述】:

为了使用 Form::select,您需要添加一个名称和两个数组。

在正常情况下,我们有一个带有 id 和另一个带有名称。两者都需要从查询中加载。

所以在模型中我有这样的东西:

public static function get_ids()
{
    //return DB::query('select id FROM __roles');
    return DB::table('roles')->get(array('id'));
}

//Returns an array with all the names
public static function get_names()
{
    //return DB::query('select name FROM __roles');
    return DB::table('roles')->get(array('name'));
}

但是,这给了我这个:

Array ( [0] => stdClass Object ( [id] => 1 ) [1] => stdClass Object ( [id] => 2 ) [2] => stdClass Object ( [id] => 3 ) )

我想得到这样的东西:

Array ( [0] => '1'  [id] => '2' [id] => '3' )

表格

Form::select('role', array(Role::get_ids(), Role::get_names())); 

【问题讨论】:

    标签: laravel fluent


    【解决方案1】:

    这行得通吗?

    public static function get_ids()
    {
        //return DB::query('select id FROM __roles');
        return DB::table('roles')->get(array('id'))->to_array();
    }
    
    //Returns an array with all the names
    public static function get_names()
    {
        //return DB::query('select name FROM __roles');
        return DB::table('roles')->get(array('name'))->to_array();
    }
    

    【讨论】:

      【解决方案2】:

      我试过这个选项:

       $projects = Project::where('user_id', Auth::user()->id)->lists('name', 'id');
      

      在模板中:

      {{ Form::select('project', $projects) }}
      

      【讨论】:

        猜你喜欢
        • 2019-05-30
        • 2012-06-07
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        • 2011-09-27
        • 2012-01-30
        • 2020-05-27
        • 2020-09-16
        相关资源
        最近更新 更多