我试图从数据库中获取值并在下拉列表中显示它。.当我尝试此操作时,我得到了此错误未定义的变量usertype ..我不明白错误在哪里..我是laravel框架的新手。

这是我的控制器:

public function get() {
    $usertype = DB::table('user_type')->get();
    return View::make('/home')->with(compact('user_type'));
    return $usertype;

}


这是我的视图下拉列表:

<div class="form-group">
                    <label for="cat">User Type</label>
                      <select class="form-control" name="user_type_id">
                        @foreach($usertype as $user)
                        <option id="user_type_Id" value="{{$user->id}}" selected="selected">{{ $user->type }}</option>
                        @endforeach
                      </select>
                    </div>


谁能帮我..

最佳答案

尝试在压缩函数中更改变量名称

public function get() {
    $usertype = DB::table('user_type')->get();
    return View::make('/home')->with(compact('usertype'));
    return $usertype;

}


并删除第二条return语句,因为它将永远不会执行。

10-05 21:08
查看更多