[英]Laravel dates on form model binding
I'm using Laravel 5.1 我正在使用Laravel 5.1
I got a model: 我有一个模型:
class ExampleModel extends Model {
// ....
protected $dateFormat = 'Y.m.d';
protected $dates = ['first_date', 'second_date'];
// ...
}
So when I'm indexing ExampleModel elements, the date format is correct (ex 2015.07.31) 因此,当我索引ExampleModel元素时,日期格式是正确的(ex 2015.07.31)
But on an edit form it uses the default format: 2015-07-31 00:00:00 但在编辑表单中,它使用默认格式:2015-07-31 00:00:00
I'm using Form::model()
binding. 我正在使用
Form::model()
绑定。
I know I could use getFirstDateAttribute()
but it's not the solution I'm looking for. 我知道我可以使用
getFirstDateAttribute()
但它不是我正在寻找的解决方案。 Because it's not elegant at all and once I defined the $dates
array, it should work automatically in every case. 因为它根本不优雅,一旦我定义了
$dates
数组,它应该在每种情况下自动工作。
So is it a bug maybe? 这可能是个错误吗? Or am I doing something wrong?
或者我做错了什么?
I solved my problem by overriding Carbon's default date format: 我通过覆盖Carbon的默认日期格式解决了我的问题:
Carbon\Carbon::setToStringFormat('Y.m.d');
But Kelly's answer is much better and more elegant, I just post this one as well, maybe someone will find this useful once. 但凯利的回答更好,更优雅,我也发布了这个,也许有人会发现这个有用一次。
I've never done this before, but it seems to work on a basic example I put together. 我以前从未这样做过,但它似乎与我放在一起的一个基本例子有关。 Note that I'm just calling the
toArray
method on the model in the form opening tag. 请注意,我只是在表单开始标记中调用模型上的
toArray
方法。
{!! Form::model($exampleModel->toArray(), ['route' => ['example-models.update', $exampleModel->id]]) !!}
{!! Form::label('first_date', 'First Date') !!}
{!! Form::text('first_date') !!}
{!! Form::close() !!}
The docs say that the dateFormat
property determines the date format when the object is cast to json or an array. 文档说
dateFormat
属性确定将对象dateFormat
转换为json或数组时的日期格式。
I don't fully advise this as a fix because when you update the core you'll lose this fix, but maybe this should be posted as a pull request to Laravel's next version. 我没有完全建议将其作为修复,因为当您更新核心时,您将失去此修复程序,但也许这应该作为拉动请求发布到Laravel的下一个版本。
In \\Illuminate\\Database\\Eloquent\\Concerns\\HasAttributes update the asDate function as follows. 在\\ Illuminate \\ Database \\ Eloquent \\ Concerns \\ HasAttributes中更新asDate函数,如下所示。 As you can see from the comments the asDate function still returns a timestamp even though it's 00:00:00.
从注释中可以看出,asDate函数仍然返回时间戳,即使它是00:00:00。
/**
* Return a timestamp as DateTime object with time set to 00:00:00.
*
* @param mixed $value
* @return \Illuminate\Support\Carbon
*/
protected function asDate($value)
{
$date = $this->asDateTime($value)->startOfDay();
$date->setToStringFormat($this->getDateFormat());
return $date;
}
This allows you to control the format of a date from the model (note I'm differentiating between a date and datetime). 这允许您从模型中控制日期的格式(注意我区分日期和日期时间)。 Then use the casts variable to cast your variable to date format.
然后使用casts变量将变量转换为日期格式。
protected $casts = [
'start_date' => 'date',
'end_date' => 'date'
];
protected $dateFormat = 'Y-m-d';
The $dates variable cannot differentiate between a date and a datetime. $ dates变量无法区分日期和日期时间。
UPDATE The real problem is the form model binding skips the helper function of the FormBuilder class. 更新真正的问题是表单模型绑定跳过了FormBuilder类的辅助函数。 As you can see if you inspect \\Collective\\Html\\FormBuilder::date
如您所见,检查\\ Collective \\ Html \\ FormBuilder :: date
/**
* Create a date input field.
*
* @param string $name
* @param string $value
* @param array $options
*
* @return \Illuminate\Support\HtmlString
*/
public function date($name, $value = null, $options = [])
{
if ($value instanceof DateTime) {
$value = $value->format('Y-m-d');
}
return $this->input('date', $name, $value, $options);
}
It is correctly formatting date to 'Ymd' as specified in HTML5 spec. 正确地将日期格式化为HTML格式中指定的'Ymd'。 However at this point $value is actually null.
但是在这一点上,$ value实际上是null。 So you have to update the generic input function.
所以你必须更新通用输入功能。
/**
* Create a form input field.
*
* @param string $type
* @param string $name
* @param string $value
* @param array $options
*
* @return \Illuminate\Support\HtmlString
*/
public function input($type, $name, $value = null, $options = [])
{
...
//$value is null here
if (! in_array($type, $this->skipValueTypes)) {
//$value is fetched based on hierarchy set by
$value = $this->getValueAttribute($name, $value);
//necessary duplicate code to format date value
if ($type == 'date' && $value instanceof DateTime) {
$value = $value->format('Y-m-d');
}
}
...
}
UPDATE There is no need to update vendor code. 更新无需更新供应商代码。 Check out FormModelAccessors
查看FormModelAccessors
https://laravelcollective.com/docs/5.2/html#form-model-binding https://laravelcollective.com/docs/5.2/html#form-model-binding
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.