日期值未正确设置

问题描述:

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');

我在我的表单中使用了两种类型的日期,一种是'Ymd H:i:s'格式,这是 对于created_at,updated_at,deleted_at和一个用于valid_to'Ymd'问题是我的created_at正在设置正确但当我尝试保存 valid_from strong>时它的值没有设置并保存 0000-00-00 strong>在数据库中我已经尝试了一些东西,但它似乎没有工作 控制器代码 p>

  $ 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; 
 //返回$ 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)  ; 
  code>  pre> 
 
 

模型代码 p>

  protected $ fillable = ['valid_from']; 
 protected $ dates =  [
'deleted_at',
 //'valid_from',
 
 
]; 
 // protected $ valid = ['valid_from']; 
 //公共函数getFormattedValidFromAttribute($ valid){\  n // $ valid = new Carbon($ valid); 
 //返回$ valid->格式('dm-Y'); 
 ////返回$ t  his-> valid_from->格式('dm-Y'); 
 //} 
 //公共函数getFormattedValidToAttribute(){
 //返回$ this-> valid_to->格式('dm  -Y'); 
 //} 
 //受保护的函数getDateFormat()
 // {
 ////返回'U'; 
 //返回'dmY'; 
 //} \  ñ代码>  PRE> 
 
 

移植 p>

  $表 - >日期( 'VALID_FROM'); 
 代码>  
  div>

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);

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');