2

I am getting errors on submitting a form to the server that has a bootstrap-datepicker field. What should I do now?

The error report is:

InvalidArgumentException in Carbon.php line 425:
Unexpected data found.
Unexpected data found.
Data missing

in database migration file:

$table->timestamp('dob'); // dob is date of birth

in my model:

protected $dates = ['dob'];

HTML:

<div class="form-group {{ $errors->has('dob') ? 'has-error' : '' }}">
   <label class="col-sm-2 control-label">Birthday*</label>
     <div class="col-sm-10">
        <input type="text" class="form-control" name="dob" required="" id="datepicker" value="{{ old("dob") }}">
        {!! $errors->first('dob','<span class="help-block">:message</span>') !!}
      </div>
 </div>

script:

<script src="{{ url('js/bootstrap-datepicker.js') }}" charset="utf-8"></script>
<script>
$('#datepicker').datepicker({
     autoclose: true
   });
</script>

3 Answers 3

2

If you're trying to get the bootstrap-datepicker jQuery plugin to work with Laravel date formats, here are the steps:

Step 1, set this format in your JavaScript:

jQuery('.datepicker').datepicker({
    format: 'yyyy-mm-dd'
});

Step 2, add the time part of the date when saving the date in your Controller, example:

$model->start_date = $request->start_date .' 00:00:00';
Sign up to request clarification or add additional context in comments.

Comments

0

The datpicker format is different from timestamp format so you need to follow one of the solutions:

1.change the accepted datepicker's format using format option

2.follow the following link to know how to change the timestamp's format to whatever you want or even make it nullable.
Laravel "Unexpected data found" error when trying to change format of Carbon created_at date

1 Comment

what if i change the timestamp to string? is that fine?
0

You need to change your field before saving in DD, In order to do that add something like this function to your relevant model, Notice that you must follow the naming convention which is //setNameAttribute . Then pass the date taken from datepicker

   public function setDob($date)
    {
      $this->attributes['dob']=Carbon::createFromFormat('Y-m-d H:i:s',$date);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.