dozabt4329
dozabt4329
2018-03-07 07:02

日期值未正确设置

已采纳

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 duanbei1598 3年前

    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);
    
    点赞 评论 复制链接分享
  • dqkf49487 dqkf49487 3年前

    You got the dd($input->valid_from); as "17-03-2018" .Its in d-m-Y format.

    Try this

    Carbon::createFromFormat('d-m-Y', $request->valid_from)->format('Y-m-d');
    
    点赞 评论 复制链接分享

为你推荐