如何在laravel 5.6中级联显示下拉菜单?

时间:2018-09-17 10:18:56

标签: php laravel laravel-5

我有3个表,即菜单,子菜单和页面。

菜单表

Schema::create('menu', function (Blueprint $table) {
    $table->increments('menu_id')->primary();
    $table->string('name');
    $table->timestamps();
});

菜单模型

protected $table = 'menu';
protected $fillable = ['name'];
protected $dates = ['deleted_at'];

子菜单表

Schema::create('submenu', function (Blueprint $table) {
    $table->increments('submenu_id')->primary();
    $table->integer('menu_id');
    $table->foreign('menu_id')
        ->references('id')
        ->on('menu')
        ->onDelete('cascade');
    $table->string('title');
    $table->timestamps();
});

子菜单模型

 protected $table = 'submenu';
protected $fillable = ['menu_id','title'];
protected $dates = ['deleted_at'];

public function menu(){
    return $this->belongsTo('App\Menu','menu_id','menu_id');
}

页表

Schema::create('page', function (Blueprint $table) {
    $table->increments('page_id');
    $table->integer('menu_id');
    $table->integer('submenu_id');
    $table->text('description');
    $table->integer('status')->default('1');
    $table->softDeletes();
    $table->timestamps();
});

页面模型

  protected $table = 'page';
protected $fillable = ['menu_id', 'submenu_id', 'description'];
protected $dates = ['deleted_at'];

PageController

addPage函数

 public function addPage(Request $request){
   if($request->isMethod('post')){
       $data = $request->all();

          $this->validate($request, [
            'menu_id' => 'required',
            'description' => 'required'
          ],
          [
            'menu_id.required' => 'Menu Name is required',
            'description.required' => 'Description is required'
        ]);

       $page = new Page();
       $submenu = new Submenu();
        $menu = new Menu();
       $page->menu_id = $data['menu_id'];
       $page->submenu_id = $data['submenu_id'];
       $page->description = $data['description'];
     // print_r($data);die;
       $page->save();

     // echo $title; ;die;
       return redirect('/admin/view-page')->with('flash_message_success','Page Added Successfully..');
   } 

   return view('admin.page.add_page');

}

addpage.blade.php

<div class="control-group">
          <label class="control-label">Menu Name</label>
          <div class="controls">
          <?php $menu = DB::table('menu')->orderBy('name')->where('status',1)->get(); ?>
            <select name="menu_id" id="menu_id">
              <option selected disabled>Select Menu Name</option>
              @foreach($menu as $data)
              <?php if($data->deleted_at==null) { ?>
              <option value="{{ $data->menu_id }}">{{ strtoupper($data->name) }}</option>
              <?php } ?>
             @endforeach
            </select>
          </div>
        </div>
        <div class="control-group">
          <label class="control-label">Submenu Title</label>
          <div class="controls">
          <?php $submenu = DB::table('submenu')->orderBy('title')->where('status',1)->get(); ?>
            <select name="submenu_id" id="submenu_id">
              <option selected disabled>Select Submenu Title</option>
              @foreach($submenu as $data)
              <?php if($data->deleted_at==null) { ?>
              <option value="{{ $data->submenu_id }}">{{ $data->title }}</option>
              <?php } ?>
             @endforeach
            </select>
          </div>
        </div>

我已经根据addsubmenu.blade.php中的菜单保存了子菜单。现在,在添加新页面时,我要根据第一个下拉菜单中的所选菜单在第一个下拉菜单中显示所有菜单,并在第二个下拉菜单中显示子菜单。

我找不到解决方案。我需要帮助来解决这个问题...

0 个答案:

没有答案