我有四张桌子:
asset_category
表(列'asset_category_id'
,'category'
), assets
表(列'asset_id'
, 'asset_category_id'
,'manufacturer_id'
,'department_id'
),
manufacturers
表(列manufacturers_id
,manufacturer
)和 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。如果你理解,请帮忙。提前谢谢。
答案 0 :(得分:0)
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}}