我想生成一个显示年份的下拉列表,到目前为止,这是我所做的。我在下面的控制器中创建了一个方法。
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
class RUMSModel extends Model
{
protected $primaryKey = 'id';
protected $fillable = [];
protected $dateFields = [];
protected $dropdownFields = [];
public $module_dir = '';
public $module = '';
public $importFields = [];
public static $months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
public function selectYear($name, $startYear, $endYear, $selected = null, $options = array())
{
$years = range($startYear, $endYear);
$years = array_combine($years, $years); // [2013 => 2013]
return $this->select($name, $years, $selected, $options);
}
?>
使用它显示在Blade模板中:
<div class="col-lg-6">
<div class="form-group">
<label for="validation-latest_estimate_year"><b>Latest Estimate Year:</b></label>
<select id="validation-latest_estimate_year" class="form-control" name="latest_estimate_year">
<option value="">-Select Year-</option>
@foreach($model->selectYear('year') as $year)
<option value="{{$year}}" {{$year == $model->latest_estimate_year ? 'selected': ''}}>{{$year}}</option>
@endforeach
</select>
</div>
</div>
但是,下拉列表不显示年份。我试图弄清楚为什么,但是我没办法了。专家的任何指导都会有所帮助。
答案 0 :(得分:0)
为什么不使用laravelcollective/html软件包中的selectYear()
?
安装:
composer require laravelcollective/html
查看/刀片:
{!! Form::selectYear('year', 1900, 2019) !!}
答案 1 :(得分:0)
大家好,感谢您抽出宝贵时间回答这篇文章。我找到了一种使其工作的方法,但我不知道这是否是最有效的方法。
这是我为使它工作而做的事情:
<div class="col-lg-6">
<div class="form-group">
<label for="validation-latest_estimate_year"><b>Latest Estimate Year:</b></label>
<select id="validation-latest_estimate_year" class="form-control"
name="latest_estimate_year">
<option value="">{{$model->latest_estimate_year}}</option>
@for ($year = date('Y') + 2; $year < date('Y') + 100; $year++)
<option value="{{$year}}" {{$year == $model->latest_estimate_year ? 'selected': ''}}>{{$year}}</option>
@endfor
</select>
</div>