L
L
Li2016-06-02 16:10:06
Laravel
Li, 2016-06-02 16:10:06

Write datetime-local format date to db?

Hello. It is necessary to record the date in a database of a certain format.
There is a text field

<input name="date_start" value="{{ isset($editEvent) ? $editEvent->date_start->format('Y-m-d\TH:i') : '' }}" type="datetime-local" class="form-control" required>

The Event model states:
protected $dates = ['date_start'];
And the controller updates the record in the database.
$editEvent = $eventModel::find($id);
$editEvent->update($request->all());

But when updating the data, an
InvalidArgumentException in Carbon.php line 425:
Unexpected data found error occurs.
Data missing

Then I added the following to the model:
protected $dateFormat = 'Y-m-d\TH:i';
after that, the update of the date_start field was successful, but the output of this field in the template caused an error.
ErrorException in Carbon.php line 425:
Unexpected data found.
Trailing data

It is necessary to write to the database the data from the datetime-local field to the database. How to solve this, or what to read? I would be very grateful for help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Inishev, 2016-06-02
@cubaPro

Remove $dateFormat from the class. Add to model

public function setDateStartAttribute($value)
   {
       $this->attributes['date_start'] = Carbon::createFromFormat('Y-m-d\TH:i', $value);
   }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question