2013-02-08 16 views
0

爲了使用Form :: select,你需要添加一個名字和兩個數組。在laravel中做出選擇的最佳方法?

在正常情況下,我們有一個與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())); 

回答

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(); 
} 
+0

不,我試過了。 – 2013-02-08 16:40:31

+0

我可能是錯的,但我認爲to_array只是爲了雄辯。 – David 2013-02-08 19:21:02

0

我試過這個選項:

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

模板:

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