Laravel 表单模型绑定 - 日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23688096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:31:28  来源:igfitidea点击:

laravel form model binding - date formatting

formsbindinglaravelmodellaravel-4

提问by Trass Vasston

I have Laravel. I have a form. I have a MySQL database. There are some dates in it. When I bind the model and the form, the form is dutifully populated with raw MySQL dates. This is obviously not what I need.

我有 Laravel。我有一个表格。我有一个 MySQL 数据库。里面有一些日期。当我绑定模型和表单时,表单会尽职尽责地填充原始 MySQL 日期。这显然不是我需要的。

My question is: How do I get a model bound form to display dates in a more readable way?!? There is not chance to intercept and format the data. Or maybe for a more general solution, is there any way to do some proessing on any data between the model and the form, before the user sees it?

我的问题是:如何获得模型绑定表单以更易读的方式显示日期?!?没有机会拦截和格式化数据。或者也许对于更通用的解决方案,有没有办法在用户看到之前对模型和表单之间的任何数据进行一些处理?

Am I thinking of this all wrong? A billion thanks!

我想这一切都错了吗?十亿感谢!

采纳答案by The Alpha

You may add an accessormethod in your model like this:

您可以在模型中添加一个访问器方法,如下所示:

public function getCreatedAtAttribute($date)
{
    $date = new \Carbon\Carbon($date);
    // Now modify and return the date
}

This will be called for created_at. Also check date-mutatorsif you need to override defaults. Check Carbon Docs.

这将被要求created_at。如果您需要覆盖默认值,还要检查date-mutators。检查碳文档

回答by ollieread

You can format the date in the form using the format()method.

您可以使用format()方法格式化表单中的日期。

If you're using it in a form element:

如果您在表单元素中使用它:

{{ Form::text('name', $model->created_at->format('d/m/Y H:i')) }}

If you're displaying it as plain text:

如果您将其显示为纯文本:

{{ $model->created_at->format('d/m/Y H:i') }}

If you would like the user to be able to modify the date in a nice way, you could use three different select fields. Here's an excerpt from a project I worked on that allows the user to change the date of birth:

如果您希望用户能够以一种很好的方式修改日期,您可以使用三个不同的选择字段。这是我参与的一个项目的摘录,该项目允许用户更改出生日期:

<div class="form-group {{ $errors->has('date_of_birth') ? 'has-error' : '' }}">
    {{ Form::label('date_of_birth', 'Date of Birth', ['class' => 'control-label']) }}
    <div class="form-inline">
        {{ Form::selectRange('date_of_birth[day]', 1, 31, null, ['class' => 'form-control']) }}
        {{ Form::selectMonth('date_of_birth[month]', null, ['class' => 'form-control']) }}
        {{ Form::selectYear('date_of_birth[year]', date('Y') - 3, date('Y') - 16, null, ['class' => 'form-control']) }}
    </div>
    {{ $errors->first('date_of_birth', '<span class="help-block">:message</span>') }}
</div>

This is contained within a form that has the model bound to it.

这包含在一个绑定了模型的表单中。

Side Note

边注

The alternative answer posted in here actually overrides the default way that laravel handles dates, and unless you actually return a carbon object, you'll never have the luxury of using the formatmethod, or any of the other handy things that carbon has.

此处发布的替代答案实际上覆盖了 laravel 处理日期的默认方式,除非您实际返回一个 carbon 对象,否则您将永远无法使用该format方法或 carbon 具有的任何其他方便的东西。

If you have other columns that contain dates, add them to the data mutators list:

如果您有其他包含日期的列,请将它们添加到数据修改器列表中:

public function getDates()
{
    return ['created_at', 'updated_at', 'date_of_birth', 'some_date_column'];
}

This will now make it so that each of those is an instance of Carbonallowing you to format as you please, whenever you please and easily modify, duplicate and a whole host of other things. For more information regarding this, see: http://laravel.com/docs/eloquent#date-mutators

现在,这将使每一个都是Carbon允许您随意格式化的实例,只要您愿意,就可以轻松修改、复制和大量其他内容。有关这方面的更多信息,请参阅:http: //laravel.com/docs/eloquent#date-mutators

回答by androsfat

You can format all fields retrieved from the database with creating an Accessorin your model. For example if your database field is created_atuse getCreatedAtAttribute.

您可以格式化从数据库创建检索到的所有领域的访问者在你的模型。例如,如果您的数据库字段是created_atuse getCreatedAtAttribute

public function getCreatedAtAttribute($date)
{
    return Carbon::($date)->format('d/m/Y');
}