Reputation: 367
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');
Upvotes: 0
Views: 118
Reputation: 361
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');
Upvotes: 0
Reputation: 659
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);
Upvotes: 1