【问题标题】:Laravel dd/mm/yyyy to yyyy/mm/dd/ conversion while using Model for DB Operations使用模型进行数据库操作时,Laravel dd/mm/yyyy 到 yyyy/mm/dd/ 的转换
【发布时间】:2015-02-21 15:33:59
【问题描述】:

这是我的控制器

public function DriverProcess()
    {            
     $DriverData = Input::all();
     $validation = Validator::make($DriverData, DriverModel::$rules);
     if ($validation->passes())
        {
            DriverModel::create($DriverData);
     //Rest of Code 

这是我的模型

<?php
class DriverModel extends Eloquent 
{


    protected $guarded = array('DriverName');
    protected $fillable = array('DriverName', 'Age', 'Address', 'LicenseNumber', 'DateOfBirth', 'LicenseExpiry' );

在DateOfBirth 和LicenseExpiry 字段中

我以dd/mm/yyyy 格式发送日期值

当我发送使用模型DriverModel::create($DriverData); 和使用fillable 时,我不知道在哪里将dd/mm/yyyy 转换为yyyy/mm/dd

注意:

我知道通过date("Y-m-d", strtotime($var) ); 转换日期格式,但我需要知道我可以在哪里进行此操作。

【问题讨论】:

    标签: php date laravel laravel-4 model


    【解决方案1】:

    您可以在您的模型中为此使用修改器(请参阅http://laravel.com/docs/4.2/eloquent#accessors-and-mutators)。在你的情况下,那将是:

    public function setDateOfBirthAttribute($value)
    {
        $this->attributes['DateOfBirth'] = date("Y-m-d", strtotime($value) );
    }
    

    作为旁注,您应该只在模型中使用$fillable 或$guarded。现在,您在$guarded 和$fillable 中设置了相同的属性。

    【讨论】:

      猜你喜欢
      • 2016-06-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-31
      相关资源
      最近更新 更多