【问题标题】:A four digit year could not be found Data missing when using protected dates in laravel在 laravel 中使用受保护的日期时,找不到四位数的年份数据丢失
【发布时间】:2020-03-19 17:15:33
【问题描述】:

我正在尝试更新 (date_of_completion) 字段,但它显示错误,如果我从模型中删除受保护的 $dates 它可以工作

错误

A four digit year could not be found
Data missing

型号

<?php

namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{

    protected $dates = ['date_of_join','date_of_completion','dob'];
    protected $table = 'student_lists';
    protected $fillable = ['student_name', 'student_registration_id', 'date_of_join',    'student_phone_no', 'student_photo','date_of_completion','dob' ];

}

输入视图

<div class="col-md-3">
<input   type="date" name="date_of_completion" class="form-control form-control-lg" id="date_of_completion"  required>
</div>

【问题讨论】:

  • 你能告诉我你编辑日期的控制器部分吗?

标签: laravel laravel-5 laravel-5.6


【解决方案1】:

添加到$dates 的所有字段都需要默认格式的日期,通常是Y-m-d H:i:s

您可以像这样更改默认格式:

protected $dateFormat = 'Y-m-d';

但它会影响所有日期。在您的情况下,最好将 date_of_completion 转换为所需的格式:

protected $casts = [
    'date_of_completion' => 'datetime:Y-m-d',
];

See docs here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-30
    • 2017-03-24
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    相关资源
    最近更新 更多