Drop down menu Laravel中的建筑下拉列表(无模型)

Drop down menu Laravel中的建筑下拉列表(无模型),drop-down-menu,view,laravel,Drop Down Menu,View,Laravel,我试图在Laravel中创建一个下拉列表,选择选项是来自我数据库的值,我遇到了一些问题。在本网站的其他教程中,“执行”下拉列表查询为数据库构建模型。我没有创建模型类,也不打算创建 Routes.php Route::get("/user/charname", array( 'as' => 'profile-character', 'uses' => 'ProfileController@dropDownList' )); ProfileController.php

我试图在Laravel中创建一个下拉列表,选择选项是来自我数据库的值,我遇到了一些问题。在本网站的其他教程中,“执行”下拉列表查询为数据库构建模型。我没有创建模型类,也不打算创建

Routes.php

Route::get("/user/charname", array(
    'as' => 'profile-character',
    'uses' => 'ProfileController@dropDownList'
));
ProfileController.php

class ProfileController extends BaseController
{
    public function dropDownList()
    {
        $list = DB::table('characters')->where('char_id', '128')->pluck('char_name');
        return View::make('layout.profile')->with('character_options',$list);
    }
}
在profile.blade.php(视图)中


您需要在视图中使用
$character\u options
而不是
$list

您实际上在
视图::make()
调用中指定变量应为“character\u options”,因此需要将其称为
$character\u options

另外,
->lists('char\u name')
比使用
->pull('char\u name')
更好,因为它会提供完整的列表。Pulk只返回它找到的第一个项目


除此之外,使用
->list('char_name','id')
可以获得由
id
列键入的列表,如果要使用此列表确定外键字段的id,这将非常有用。如果不是,那也没什么大不了的。

我认为,通过这样做,我已经部分解决了这些问题:

 {{ Form::select('list', DB::table('characters')->where('char_id', '128')->lists('char_name') ,Input::old('list')) }}
我个人并不认为这是一个可行的解决办法,而是一个暂时的办法

{{Form::select('character\u options',$character\u options,Input::old('character\u options'))}

错误:未定义的变量字符\u选项

{{Form::select('character\u options',$list,Input::old('character\u options'))} 错误:未定义的变量列表


可能我不明白如何将变量从控制器发送到视图。

您需要使用@alexrussel提到的
列表。请注意,
View::make('layout.profile')->带有('character_options',$list)
,您正在传递变量
$character\u options
,其值包含在
$list
中,并且
$list
将不再对视图可用

ProfileController.php

<?php

class ProfileController extends BaseController
{
    public function dropDownList()
    {
        $list = DB::table('characters')->where('char_id', '128')->lists('char_name');
        return View::make('layout.profile')->with('character_options',$list);
    }
}
<div class="selected_char">
<form action="{{ URL::route('profile-character') }}" method="post">
    <li>
        {{ Form::select('character_options', $character_options ,Input::old('character_options')) }}
    </li>
</form>

我用{{Form::select('character\u options',$character\u options,Input::old('character\u options'))}对代码进行了必要的修改。你说的很有道理,但我不明白为什么它仍然不起作用。它说现在character_options在profile.blade.phpSomething中是未定义的变量,就像{{Form::select('character_options',$character_options,Input::old('character_options'))}在我看来?是的,这听起来很合理,但从你的其他评论来看,这对你不起作用。如果您实际粘贴了控制器或视图代码的简化版本,您可以尝试提供更多详细信息-可能有什么东西干扰了变量的可用性,但我们无法看到它,因为为了简洁起见,您删除了一些代码。也许你可以将你的控制器和视图简化为最简单的形式,然后逐步升级——你通常会看到导致大问题的一个小问题。
<?php

class ProfileController extends BaseController
{
    public function dropDownList()
    {
        $list = DB::table('characters')->where('char_id', '128')->lists('char_name');
        return View::make('layout.profile')->with('character_options',$list);
    }
}
<div class="selected_char">
<form action="{{ URL::route('profile-character') }}" method="post">
    <li>
        {{ Form::select('character_options', $character_options ,Input::old('character_options')) }}
    </li>
</form>