dozabt4329 2018-03-07 07:02
浏览 28
已采纳

日期值未正确设置

i am using two types of dates in my form one with the 'Y-m-d H:i:s' format which is for created_at,updated_at,deleted_at and one is for valid_to 'Y-m-d' the problem is my created_at are getting set properly but when i try to save the valid_from it's value doesn't get set and it saves 0000-00-00 in the database i have tried a couple of things but non of it seems to be working Controller code

 $input = $request->all();
//        if($request->input('valid_from')) {
//            $date = explode("-", $request->input('valid_from'));
//            $dob = $date['2'] . '-' . $date['1'] . '-' . $date['0'];
//        }
//        $input->valid_from = $dob;
//        return $request->all();
//        $date= $input->valid_from;
//        $date = \Carbon\Carbon::parse($request->current_date);
//
//        $day = $date->day;
//        $month = $date->month;
//        $year = $date->year;
//        dd($request);
//        $date = $input->valid_from;
//        $input->valid_from = date('M d,Y',$date);
////        $date = explode('-', $input->valid_from);
////        $input->valid_from = $date['2'].'-'.$date['1'].'-'.$date['0'];
        Promotion::create($input);

Model code

protected $fillable = ['valid_from'];
    protected $dates = [
        'deleted_at',
// 'valid_from',


    ];
//    protected $valid = ['valid_from'];
//    public function getFormattedValidFromAttribute($valid) {
//        $valid = new Carbon($valid);
//        return $valid->format('d-m-Y');
////        return $this->valid_from->format('d-m-Y');
//    }
//    public function getFormattedValidToAttribute() {
//        return $this->valid_to->format('d-m-Y');
//    }
//    protected function getDateFormat()
//    {
////        return 'U';
//        return 'd-m-Y ';
//    }

Migration

$table->date('valid_from');
  • 写回答

2条回答 默认 最新

  • duanbei1598 2018-03-07 07:55
    关注

    You have to make sure that you followed the date_format of your column valid_from.

    $input = $request->except('valid_from');
    $input['valid_from'] = date('Y-m-d',strtotime($request->input('valid_from')));
    Promotion::create($input);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入