这是我的控制器:
public function create(){
$categories =DB::select('select Code from ItemCategory');
return view('item')->with('ItemCategory', $categories);
我的视图文件:
<div class="form-group">
{!! Form::label('Link Category') !!}<br />
{!! Form::select('categories',
(['0' => 'Select a Category'] + $categories),
null,
['class' => 'form-control']) !!}
</div>
但是当我运行它时,出现以下错误
FileViewFinder.php第137行中的InvalidArgumentException:未找到视图[item]。
PS:
我的数据库表:
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| ID | int(11) | NO | PRI | NULL | |
| Code | varchar(45) | NO | | NULL | |
| ItemCategotyID | int(11) | NO | | NULL | |
| ItemLevelID | int(11) | NO | | NULL | |
| isActive | varchar(45) | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+
我已经试过这个例子。这是什么错误?如何使用MySQL表获取下拉列表的值?
要将值加载到下拉列表中,必须使用route.php。
Route::get('additem',function(){
$categories = ItemCategory::all();
return view('***/***.add_item')->with('categories',$categories);
});
您必须将模型包括在route.php中
use App\ItemCategory;
然后在您的视图文件中,使用原始html。
<div class="form-group">
{!! Form::label('Category', 'Category:') !!}
<select class="form-control input-sm" name="">
@foreach($categories as $cats)
<option value="{{$cats->ID}}">{{$cats->Code}}</option>
@endforeach
</select>
</div>
这是dropdown
列表从数据库中获取值之后的样子:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句