Laravel ErrorException,执行多个动态下拉列表

时间:2017-11-07 22:46:49

标签: php laravel laravel-5.4 laravel-routing laravel-blade

我有四张桌子: asset_category 表(列'asset_category_id''category'), assets 表(列'asset_id''asset_category_id''manufacturer_id''department_id'), manufacturers 表(列manufacturers_idmanufacturer)和 departments 表(列department_id,{ {1}})。 department表列与另外三个表链接。我在表单中执行动态下拉列表以插入asset表格

Asset.blade.php

asset

AssetController

<form action="{{url('/insertAsset')}}" method="POST" edata-parsley-validate novalidate>
    {{ csrf_field() }}
    <div class="form-group">
        <label for="userName">Asset ID*</label>
        <input type="text" name="asset_id" parsley-trigger="change" required placeholder="Asset ID" class="form-control" id="userName" disabled>
    </div>
    <div class="form-group">
        <label for="userName">Asset Category ID*</label>
        <select name="asset_category_id" parsley-trigger="change" required placeholder="Asset Category ID" class="form-control">
       <option>Select an Asset Category</option>
        @foreach( $asset_categories as $asset_category )
            <option value=" {{$asset_category->asset_category_id}}"> {{ $asset_category->category }} </option>
                @endforeach
        </select>
    </div>
    <div class="form-group">
        <label for="userName">Asset Manufacture ID*</label>
        <select name="manufacturer_id" parsley-trigger="change" required class="form-control">
            <option>Select a Manufacturer</option>
                @foreach( $manufacturers as $manufacturer )
                    <option value=" {{$manufacturer->manufacturer_id}}"> {{ $manufacturer->manufacturer }} </option>
                @endforeach
            </select>
    </div>
    <div class="form-group">
        <label for="userName">Department ID*</label>
        <select name="department_id" parsley-trigger="change" required placeholder="Department ID" class="form-control" >
            <option>Select a Department</option>
            @foreach( $departments as $department )
                <option value=" {{$department->department_id}}"> {{ $department->department }} </option>
            @endforeach
        </select>
    </div>

<div class="form-group text-right m-b-0">
<button class="btn btn-primary waves-effect waves-light" type="submit">
    Submit
</button>
</div>

Web.php

<?php

namespace App\Http\Controllers;

use App\Asset;
use App\Asset_category;
use App\Manufacturer;
use App\Department; 

use Illuminate\Http\Request;

class AssetController extends Controller
{   
    public function asset_category(){
        $asset_categories = Asset_category::all();
    return view('asset', ['asset_categories' => $asset_categories]);
    }

    public function manufacturer(){
        $manufacturers = Manufacturer::all();
        return view('asset', ['manufacturers' => $manufacturers]);
    }

    public function department(){
        $departments = Department::all();
        return view('asset', ['departments' => $departments]);
    }    

}

运行应用程序时,我得到一个<?php Route::get('/asset', function (){ return view('asset'); } ); Route::get('/asset', 'AssetController@asset_category'); Route::get('/asset', 'AssetController@department'); Route::get('/asset', 'AssetController@manufacturer'); /*POST */ Route::post('/insertAsset', 'AssetController@add'); ,其中声明:

ErrorException

经过全面测试后我发现,只有一个下拉显示,因为laravel只从Undefined variable: asset_categories (View: C:\xampp\htdocs\laravel_app\resources\views\asset.blade.php). 中的3个函数中的一个函数路由一个变量,而其他函数中的另外两个变量不发送到web.php。所以现在我想要一种方法将其他变量路由到asset.blade.php。如果你理解,请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:0)

  • 首先,Laravel只匹配具有特定ID和特定类型的第一条路线。在这种情况下,只会找到第一条路线public function asset(){ $asset_categories = Asset_category::all(); $manufacturers = Manufacturer::all(); $departments = Department::all(); return view('asset', ['asset_categories' => $asset_categories, 'manufacturers' => $manufacturers, 'departments' => $departments]); }
  • 做我想不到的事。您只需要创建一个方法,将三个变量返回到视图

    Route::get('/asset', 'AssetController@asset');

    只有一条路线 {{1}}