Laravel "Unexpected data" error while trying to change format Carbon created_at date

When I try to change the default format of the created_at field in my resource model, I get the following error:

{ "error":{ "type":"InvalidArgumentException", "message":"Unexpected data found. Unexpected data found. The separation symbol could not be found Unexpected data found. A two digit second could not be found", "file":"\/var\/www\/html\...vendor\/nesbot\/carbon\/src\/Carbon\/Carbon.php", "line":359 } } 

Here is the code that caused the error above:

 $tile = Resource::with('comments, ratings')->where('resources.id', '=', 1)->first(); $created_at = $tile->created_at; $tile->created_at = $created_at->copy()->tz(Auth::user()->timezone)->format('F j, Y @ g:i A'); 

If I remove ->format('F j, Y @ g:i A') from the above code, it works fine, but not in the format I want. What is the problem? I have an almost identical code elsewhere in my application and it works without errors.

UPDATE: Using setToStringFormat('F j, Y @ g:i A') does not raise an error, but returns null .

+6
source share
4 answers

Adding the following code to my model for me:

 public function getCreatedAtAttribute($date) { if(Auth::check()) return Carbon\Carbon::createFromFormat('Ymd H:i:s', $date)->copy()->tz(Auth::user()->timezone)->format('F j, Y @ g:i A'); else return Carbon\Carbon::createFromFormat('Ymd H:i:s', $date)->copy()->tz('America/Toronto')->format('F j, Y @ g:i A'); } public function getUpdatedAtAttribute($date) { return Carbon\Carbon::createFromFormat('Ymd H:i:s', $date)->format('F j, Y @ g:i A'); } 

This allows me to use created_at and updated_at in the right format.

+9
source

I had the same problem, and looking for an answer I came across How do you explain the result for the new \ DateTime ('0000-00-00 00: 00:00')? .

I decided to change the datetime columns in the database to nullable with the default value = NULL so that the fields would not have the value "0000-00-00 00:00:00".

My migration in laravel 5 looks like this:

 Schema::table('table', function($table) { $table->dateTime('created_at')->nullable()->default(null)->change(); $table->dateTime('updated_at')->nullable()->default(null)->change(); }); 
+5
source

This is not a carbon issue, it is a conflict between setAttribute or getAttribute in your model.

+5
source

You should not try to change the format of created_at . It must be a Carbon object. If you want to display the created_at date in a different format, just format it during its output. Or you can create a method that changes the format so that you can call it whenever you want in a different format. For example, add this method to your resource class:

 public function createdAtInMyFormat() { return $this->created_at->format('F j, Y @ g:i A'); } 

You can also set this function to time zone, etc. Then you can use $tile->createdAtInMyFormat() , for example, to get the special created_at format from your $tile object.

+2
source

Source: https://habr.com/ru/post/973598/


All Articles