我想简化laravel中的下拉菜单,所有建议都已打开:)
这是我的迁移代码:
Schema::create('profile', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->enum('hobby', ['Reading','Swimming','Other'])->default('Reading');
这是我用于创建菜单下拉菜单的视图代码(create.blade.php)
<div class='form-group col-md-6'>
<label class='control-label'>Hobby <span class="text-danger"></span></label>
<select class='form-control select2' name='hobby'>
<option value="Reading" selected="">Reading</option>
<option value="Swimming" selected="">Swimming</option>
<option value="Other" selected="">Other</option>
到目前为止,此代码可以正常工作,但是如果我使用@foreach使其更简单,则可能吗?如何?或其他建议?
答案 0 :(得分:0)
在控制器的create方法中,获取所有可能的hobie,并将它们传递给视图:
$hobies = ['Reading','Swimming','Other'];
return view('create', compact('hobies'));
在视图中:
<div class='form-group col-md-6'>
<label class='control-label'>Hobby <span class="text-danger"></span></label>
<select class='form-control select2' name='hobby'>
@foreach ($hobies as $hoby)
<option value="{{$hoby}}" selected="">{{$hoby}}</option>
@endforeach