Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bhhussain's avatar

Date format working in store but in edit there is error

Hi,

The below code working in storing the data but it is not working when I edit the data


$date  = Carbon::createFromFormat('d-m-Y', $request->th_bill_dt);        
$account->th_bill_dt = $date;  

It is saying the below error

Unexpected data found.
 Unexpected data found.

Any one can you please help me?

Thank you

0 likes
6 replies
Nakov's avatar

@bhhussain in your Account model you can add this:

protected $dates = ['th_bill_dt'];

which will automatically make your date field a carbon instance.

bhhussain's avatar

@nakov Thank you very much for your reply.

I added the code but still error is there

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class account extends Model
{
    
    protected $dates = ['th_bill_dt'];
    public function getRouteKeyName()
{
    return 'th_tran_no';
    
}

public function item()
{
    return $this->hasmany('App\item');
}

}
Nakov's avatar

@bhhussain can you please show me the code where you get this error from?

What type is your field in the database as well? Is it just a date field or a datetime?

bhhussain's avatar

Edit View Page

    <div class="form-group">
     <div class = "row">
     <label class = "col-lg-1" for="">Bill Date *</label>
     <div class = "col-lg-2">
     <input class = "form-control" id="datepicker"  name = "th_bill_dt" value="{{ date('d-m-Y', strtotime($account->th_bill_dt)) }}" placeholder="Enter bill date" required>
    <script>
        $('#datepicker').datepicker({
            uiLibrary: 'bootstrap4'
        });
    </script></div>

Only date field

Error

InvalidArgumentException
Unexpected data found.
 Unexpected data found.
Nakov's avatar
Nakov
Best Answer
Level 73

@bhhussain so the error is here most probably:

{{ date('d-m-Y', strtotime($account->th_bill_dt)) }}

Try this instead:

{{ $account->th_bill_dt->format('d-m-Y') }}

Since the th_bill_dt is already a Carbon instance when you have the $dates property in the model

Please or to participate in this conversation.