使用Laravel 5.2的动态下拉列表

时间:2016-08-04 08:11:29

标签: php mysql laravel-5

我有数据库架构:

CREATE TABLE IF NOT EXISTS `zone` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `zone_name` varchar(200) NOT NULL,
  PRIMARY KEY (`id`)
);
INSERT INTO `zone` (`id`, `zone_name`) VALUES
(1, 'first'),
(2, 'second');

CREATE TABLE IF NOT EXISTS `error` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `errorID` int(11) NOT NULL,
  `error_name` varchar(200) NOT NULL,
  PRIMARY KEY (`id`)
);

INSERT INTO `error` (`id`, `errorID`, `error_name`) VALUES
(1, 1, 'test1'),
(2, 1, 'test2'),
(3, 1, 'test3'),
(4, 1, 'test4'),
(5, 2, 'test5');

MyController.php

class myController extends Controller {

    public function zoneMethod()
    {
        $zone = DB::table('zone')->get();

         return View::make('myview', ['zone' =>'zone']);

    }

    public function errorMethod($id)
    {
        $error = DB::table('error')->where('errorID', $id)->get();
        return View::make('thisview', ['error' => 'error']);
    }
}

routes.php文件

Route::get('myroute','myController@zoneMethod');
Route::get('my/{id}','myController@errorMethod');

我有

View.php

<label for="zones">Zone</label>
<select name="parent_zone" id="parent_zone">

    @foreach($zone as $zones)
    <option value="{{ $zones->id }}">{{ $zones->zone_name }}</option>
    @endforeach

</select>
<br/><br/>

<label for="errors">Error</label>
<select name="sub_error" id="sub_error">

        @foreach($error as $errors)
         <option value="{{ $errors->id }}">{{ $errors->error_name }}</option>
        @endforeach

<br/><br/>
</select>
</div> 

我有错误 - “未定义的变量:区域......”,请帮帮我。

1 个答案:

答案 0 :(得分:1)

编辑控制器中的代码:

return View::make('myview', ['zone' => $zone]);

而不是

return View::make('myview', ['zone' =>'zone']);