duan62819774 2017-05-18 12:40
浏览 42
已采纳

too long

mySQL DATE format is Y-m-d For localization reasons, users submit date fields to my Laravel 5.4 application in the d-m-Y form, which are saved to a detenutodal field in mySQL.

I used mutators to convert the two formats, in my Attore model, like this.

<?php
namespace App;

//use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Attore extends Model
{

    protected $dates = [
        'created_at',
        'updated_at',
        'deleted_at',
        'detenutodal'
    ];

    /**
     * detenutodal: DB Y-m-d => m-d-Y
     *
     * @param  date  $value
     * @return date
     */
    public function getDetenutodalAttribute($value)
    {
        return Carbon::parse($value)->format('d-m-Y');
    }

    /**
     * detenutodal: m-d-Y => DB Y-m-d 
     *
     * @param  date  $value
     * @return date
     */
    public function setDetenutodalAttribute($value)
    {
        return Carbon::parse($value)->format('Y-m-d');
    }
}

as you may see, the detenutodal attribute is added to the $dates to make it into a Carbon instance (not declaring it seems to make no difference. Actually I was unable to have detenutodal behave like a Carbon instance, and I had to explicitly call Carbon::parse)

I create a new Attore model in AttoreController like this:

$this->validate(request(), [
    'detenutodal' =>'nullable|date_format:"d-m-Y"',
    ]);

Attore::create(request()->all());

The detenutodal field is correctly validated, and I am sure the mutator is called as I dd'ed its invocation. the correct detenutodal value is passed to it - e.g. 30-06-1943 and it returns 1943-06-30.

but the create fails with this error:

SQLSTATE[HY000]: General error: 1364 Field 'detenutodal' doesn't have a default value (SQL: insert into `attores` (`nome`, `enteopersona`, `paternita`, `maternita`, `annonascita`, `luogonascita`, `residenza`, `professione`, `gradoistruzione`, `updated_at`, `created_at`) values (Aminto Pestalozzi, persona, Agenore Pestalozzi, Giacinta Tortelazzi, 1910, Caorso in Strà' lva', Via Morigi 4, Caorso, Bracciante, Scuola professionale, 2017-05-18 14:23:22, 2017-05-18 14:23:22))

As you may see, mySQL complains that detenutodal has no default value as detenutodal has completely disappeared from the query, while I would expect that the mutator correctly updated the field before the invocation of Attore::create and added it to the query.

Can anyone shed some light?

Thanks

  • 写回答

3条回答 默认 最新

  • dousou1967 2017-05-18 14:45
    关注

    Edit : Your mutator is wrong.

    public function setDetenutodalAttribute($value)
    {
        $this->attributes['detenutodal'] = Carbon::parse($value)->format('Y-m-d');
    }
    

    You need to set the fillable property.

    protected $fillable = ['detenutodal'];
    

    or set the guarded to so

    protected $guarded = [];
    

    You allow nullable value for the date, is this field nullable?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用