0

为了使用 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())); 
4

2 回答 2

0

这行得通吗?

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();
}
于 2013-02-08T13:59:01.550 回答
0

我试过这个选项:

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

在模板中:

{{ Form::select('project', $projects) }}
于 2013-07-17T20:36:43.903 回答