提交表格中的Laravel日期格式(d-M-Y) [英] Laravel date format in submission form (d-M-Y)

查看:1098
本文介绍了提交表格中的Laravel日期格式(d-M-Y)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在刀片模板中的提交表单中,我有以下日期表,它的默认日期像Ymd一样正常。

In the submit form in the blade template I have the following date form and it works fine with default date like Y-m-d.

但是我想显示日期dMY,我试图找到一个有用的解决方案,运气

But I want to show the date like d-M-Y, I have tried to find a usable solution with out luck

以下是使用默认日期的代码:

Here is the code that works with default date:

这是模型

public static $rules = [
    'birthday'   => 'date'
];

protected $fillable = ['birthday'];

这是Controller方法

Here is the Controller method

public function update($id)
{
    $kid = Kid::findOrFail($id);

    $validator = Validator::make($data = Input::all(), Kid::$rules);

    if ($validator->fails())
    {
        return Redirect::back()->withErrors($validator)->withInput();
    }

    $kid->update($data);
    return Redirect::to('kids/list');
}

这是刀片模板

{{ Form::model($kid, ['method' => 'PATCH', 'route' => ['kids.update', $kid->id], 'files' => true], ['class' => 'form-horizontal']) }}
{{ Form::text('birthday', null, ['class' => 'form-control']) }}
{{ Form::submit('Confirm update', ['class' => 'btn btn-primary']) }}
{{ Form::close() }}






更新

这适用于所有Laravel版本从4.2和更新版本。

This works for all Laravel versions from 4.2 and newer.

对于Laravel 5.1和更新版本,您将需要将其安装为单独的软件包,因为它不再是部分的Laravel 5核心。

For Laravel 5.1 and newer, you will need to install it as a separate package as it's no longer part of the Laravel 5 core.

决定了HTML /表单构建器不是框架的核心要求,所以它从内核中删除。我链接的软件包是L4核心软件包,并被维护为一个独立的L5兼容和使用包,所以如果你选择这样做,你可以安全使用。

It was decided that the html/form builder are not a core requirement for the framework so it was removed from the core. The package I linked to is the one that was included with the L4 core and is being maintained as a separate package for L5 compatibility and usage, so you're safe using that if you choose to do so.

感谢@Bogdan进行此更新。

Thanks to @Bogdan for this update.

推荐答案

如果您需要一个数据库存储的日期格式另一个用于用户交互,您需要在显示该文件之前将其进行处理,然后再将其存储到数据库中。所以你应该这样做:

If you need one date format for db storage and another for the user interaction, you need to process it before you display it and before you store it into the database. So you should do something like this:

1。转换表单输入的日期值:

{{ Form::text('birthday', date('d-M-Y', strtotime($kid->birthday)), ['class' => 'form-control']) }}

2。在保存之前将其转换为数据库所需的格式:

public function update($id)
{
    $kid = Kid::findOrFail($id);

    // Convert birthday format to be compatible with database ISO 8601 format
    $data = Input::all();
    $data['birthday'] = date('Y-m-d', strtotime($data['birthday']));

    $validator = Validator::make($data, Kid::$rules);

    if ($validator->fails())
    {
        return Redirect::back()->withErrors($validator)->withInput();
    }

    $kid->update($data);
    return Redirect::to('kids/list');
}

如果您希望以面向对象的方式处理日期,您可以使用 Carbon 已被Laravel纳入。

If you want a more object oriented way of handling dates you can use Carbon which is already included by Laravel.

这篇关于提交表格中的Laravel日期格式(d-M-Y)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆