Laravel ErrorException,执行多个动态下拉
Posted
技术标签:
【中文标题】Laravel ErrorException,执行多个动态下拉【英文标题】:Laravel ErrorException, Performing Multiple dynamic drop down 【发布时间】:2018-04-20 11:43:04 【问题描述】:我有四张桌子:
asset_category
表(列'asset_category_id'
,'category'
),一个assets
表(列'asset_id'
,'asset_category_id'
,'manufacturer_id'
,@987654328 @),
manufacturers
表(列manufacturers_id
、manufacturer
)和departments
表(列department_id
、department
)。 asset
表列与其他三个表链接。我正在表单中执行动态下拉以插入到asset
表中
Asset.blade.php
<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>
资产控制器
<?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]);
Web.php
<?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
,它指出:
Undefined variable: asset_categories (View: C:\xampp\htdocs\laravel_app\resources\views\asset.blade.php).
经过彻底的测试,我发现,只有一个下拉菜单显示,因为 laravel 仅从 web.php
的 3 个函数中的一个函数路由一个变量,而其他函数的另外两个变量没有发送到 @ 987654343@。所以现在我想要一种将其他变量路由到asset.blade.php 的方法。如果您理解它,请帮助。提前致谢。
【问题讨论】:
【参考方案1】: 首先,Laravel 只会匹配第一个具有特定 ID 和特定类型的路由。在这种情况下只会找到第一条路线GET /asset
。
做我不明白你想做的事。您只需要创建一个方法,它将三个变量返回到视图
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');
【讨论】:
以上是关于Laravel ErrorException,执行多个动态下拉的主要内容,如果未能解决你的问题,请参考以下文章
php artisan serve [ErrorException] laravel
updateOrCreate 在 laravel 中导致 ErrorException
laravel - ErrorException 数组到字符串的转换
laravel 5.5:php artisan tinker:ErrorException:目录不为空[重复]