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.

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

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功