在droplist laravel 4中使用数据库信息

时间:2014-06-09 21:52:41

标签: laravel

我遵循了本教程:

//你们中的许多人可能已经知道这一点了,但是对于那些不能用于构建下拉菜单的人来说

// Suppose you want to show a categories dropdown whose information is pulled from an Eloquent model
// You can use the query builder's "lists" method (available for Eloquent as any query builder method) which
// returns an associative array you can pass to the Form::select method

$categories = Category::lists('title', 'id');

// note that the parameters you pass to lists correspond to the columns for the value and id, respectively
// in this case, "title" and "id"

// you should pass this data to your view and then build the dropdown inside it


//somewhere in your view
{{ Form::select('category', $categories) }}

现在,我把一切都搞定了,但我遇到了一个问题。它不返回Name,而是返回下拉列表的编号..

用户控制器中的

public function postCreatelist() {
    $userid = Auth::user()->id;
    $animename = Input::get('listname');
    $episodesseen = Input::get('episodes');
    User::find($userid)->episodes()->insert(array(
    'name' => $animename,
    'author_id' => $userid,
    'episodeswatched' => $episodesseen
    ));
    return Redirect::to('users/dashboard')->with('message', 'Added anime to list');
}
public function getAddlist() {
    $list = DB::table('animes')->orderBy('name', 'asc')->get();
    $listname = Animes::lists('name');

    $this->layout->content = View::make('users.addlist', array('list'=>$list, 'listname'=>$listname));
}

查看:

{{ Form::select('listname', $listname) }}

所以,我需要返回名称,但它返回下拉列表的编号。因此,如果我选择第一个选项,则返回0.第二个选项返回1.但它应返回名称..

我希望我能清楚地解释清楚。感谢

1 个答案:

答案 0 :(得分:0)

您可以更改第二个参数,因为这是结果数组的键。

$categories = Category::lists('title', 'title');