我在出生日期,日,月和年中有3个字段,作为3个下拉列表。我想将它们存储在数据库的单个Date_of_birth
列中。
如何在中间件中解决该问题?
public function dob($request, Closure $next) {
$dobrequest -> all();
if (isset($dobrequest['day'] && $dobrequest['month'] && $dobrequest['year'])) {
$dobrequest['dob'] = $dobrequest['day']."/".$dobrequest['month']."/". $dobrequest['year'];
}
$request ->replace($dobrequest);
return $next($request);
}
register.blade.php
<div class="col-md-6 pull-left" style="padding-left:0px;">
<font size="3"> <label for="dob">Date of Birth</label><br class="clearfix" />
<div class="form-group clearfix">
<label for="dob_month" class="sr-only">Month</label>
<select name = "dob" id="m" class="form-control">
<option>Month</option>
@for ($month = 1; $month <= 12; $month++) <option value = "{{ $month }}">{{ $month }}</option> @endfor
</select>
</div>
<div class="form-group">
<label for="dob_day" id="d" class="sr-only">Day</label>
<select name = "dob" class="form-control">
<option>Day</option>
@for ($day = 1; $day <= 31; $day++) <option value = "{{ $day }}">{{ $day }}</option> @endfor
</select>
</div>
<div class="form-group">
<label for="dob_year" id="y" class="sr-only">Year</label>
<select name = "dob" class="form-control">
<option>year</option>
@for ($year = 1950; $year <= 2052 ; $year++) <option value = "{{ $year }}">{{ $year }}</option> @endfor
</select>
</div>
</font>
</div>
protected function create(array $data)
{
dd($data);
return User::create([
'fname' => $data['fname'],
'lname' => $data['lname'],
'email' => $data['email'],
'zipcode' => $data['zipcode'],
'password' => Hash::make($data['password']),
'gender' => $data['gender'],
'dob' => $data['day'],$data['month'],$data['year'],
'health' => $data['health'],
]);
}
受保护的$ fillable = [ 'fname','lname','email','zipcode','password','gender','dob','health', ];
Class CreateUsersTable扩展了迁移 { / ** *运行迁移。 * * @返回无效 * / 公共功能up() { Schema :: create('users',function(Blueprint $ table){ $ table-> bigIncrements('id'); $ table-> string('fname'); $ table-> string('lname'); $ table-> string('email')-> unique(); $ table-> timestamp('email_verified_at')-> nullable(); $ table-> string('zipcode'); $ table-> string('password'); $ table-> string('gender'); $ table-> DATE('dob'); $ table-> string('health'); $ table-> rememberToken(); $ table-> timestamps(); }); }
];
答案 0 :(得分:1)
您可以创建将日期,日期和年份字符串格式化为日期格式的帮助器类 并定义如下功能:
public function formatDate($year, $month, $day) {
return strftime("%F", strtotime($year."-".$month."-".$day));
}
答案 1 :(得分:1)
public function handle($request, Closure $next)
{
$dobrequest = $request->all();
if(isset($dobrequest['year']) && isset($dobrequest['month']) && isset($dobrequest['day'])){
$dobrequest['dob'] = $dobrequest['year'].'-'.$dobrequest['month'].'-'. $dobrequest['day'];
}
$request->replace($dobrequest);
return $next($request);
}
}
答案 2 :(得分:0)
您可以使用参数天和年字符串创建包装函数,并定义如下函数:
function wrapDate($day, $month, $year) {
return sprintf("%d/%d/%d", $day, $month, $year);
}