doushi1912 2016-12-07 17:52
浏览 141
已采纳

Laravel:Carbon.php第425行中的InvalidArgumentException:数据丢失

When I run the system, there is exist error like this :

InvalidArgumentException in Carbon.php line 425: Data missing

    in Carbon.php line 425
    at Carbon::createFromFormat('Y-m-d H:i:s', '10-12-2016') in Model.php line 2990
    at Model->asDateTime('10-12-2016') in Model.php line 2944
    at Model->fromDateTime('10-12-2016') in Model.php line 2872
    at Model->setAttribute('tglkop', '10-12-2016') in Model.php line 442

And my model is like this :

<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class P3 extends Model
{
    use SoftDeletes;

    public $table = 'p3_s';

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

    protected $dates = ['deleted_at', 'tglkop', 'tglp3'];

    public $fillable = [
        'id_pagu',
        'id_p3',
        'kdbulan',
        'thang',
        'nokop',
        'tglkop',
        'tglp3'
    ];

    protected $casts = [
        'id_pagu' => 'string',
        'id_p3' => 'string',          
        'kdbulan' => 'string',
        'thang' => 'string',
        'nokop' => 'string'
    ];

    public static $rules = [
        'id_pagu' => 'required',
        'id_p3' => 'required',
        'kdbulan' => 'required',
        'thang' => 'required',
        'nokop' => 'required',
        'tglkop' => 'required',
        'tglp3' => 'required',
    ];


}

My table is like this :

id, int(10)
id_pagu, varchar(18)
id_p3, varchar(18)
kdbulan, varchar(2)
thang, varchar(4)
nokop, varchar(10)
tglkop, date
tglp3, date
created_at, timestamp
updated_at, timestamp
deleted_at, timestamp

I read some tutorial in stackoverflow and laravel docs, but I'm still confused to implement it

Is there any solution to solve my problem?

  • 写回答

1条回答 默认 最新

  • douyong4623 2016-12-07 17:55
    关注

    Just remove 'tglkop', 'tglp3' from your $casts array, and add this to '$dates' array. These properties will be automatically converted to Carbon instances

    UPDATE

    I guess that your db columns tglkop and tglp3 have DATE type instead of DATETIME.

    This error

    Carbon::createFromFormat('Y-m-d H:i:s', '10-12-2016') in blah blah blah
    

    says that Carbon expects date with format Y-m-d H:i:s but you gives date in format d-m-Y (10-12-2016)

    Solution 1: change type of these columns to DATETIME

    Solution 2: add that in your model

    protected $dateFormat = 'Y-m-d'; // this is expected format for `MySQL` `DATE` type field
    

    UPDATE 2

    I think you tries to create row in database. What data are you trying to pass into? I think tglkop and tglp3 fields in your input have wrong format.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)