使用laravel 4在下拉列表中显示数据库获取值

时间:2014-07-22 10:53:41

标签: laravel-4

你好我遇到大麻烦请帮我讲一下使用laravel 4下拉数据库获取值显示我的代码在下面

   <?php //echo "<pre>"; print_r($user);exit; ?>
            {{ Form::select($user, $user ) }}

in print_r($ user);显示从数据库中获取所有数据库值但我想只显示value = id,我尝试每个用户名字段但不知道如何显示value = id,用户名已归档

,结果显示在下拉列表中

 <select name="[{"id":"1","username":"skaka","name":"sandip","avatar_id":"1","gender":"1","dob":"1989-10-20","grade":"A","school":"kbpv","created_at":"2014-07-22 08:45:59","updated_at":"2014-07-22 08:48:04","parent_email":""},{"id":"2","username":"ttt","name":"test2","avatar_id":"2","gender":"2","dob":"1989-10-20","grade":"A","school":"kbpv1","created_at":"2014-07-22 08:58:25","updated_at":"2014-07-22 08:58:25","parent_email":""}]">

1 个答案:

答案 0 :(得分:0)

要使用{{Form::select()}},最简单的方法是使用lists()方法从数据库调用数据。

所以

$users = User::lists('username', 'id');

现在你有一个包含这样数据的数组:

$users = [1 => 'skaka', 2 => 'ttt', ....];

因此,您可以通过以下方式有效地使用Form::select()

{{Form::select('dropdown_name', $users)}}