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>
Noob Coder
  • 2,816
  • 8
  • 36
  • 61

3 Answers3

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';
2Fwebd
  • 2,005
  • 2
  • 15
  • 17
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

Community
  • 1
  • 1
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);
    }
Mahdi Younesi
  • 6,889
  • 2
  • 20
  • 51