chi*_*t24 10 laravel-4 php-carbon
当我尝试修改我的资源模型的默认created_at字段的格式时,我收到以下错误:
{
"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
}
}
Run Code Online (Sandbox Code Playgroud)
以下是产生上述错误的代码:
$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');
Run Code Online (Sandbox Code Playgroud)
如果我->format('F j, Y @ g:i A')从上面的代码中删除它,它工作正常,但它不是我想要的格式.问题是什么?我的应用程序中的其他地方几乎完全相同的代码,它可以正常工作.
更新:
使用setToStringFormat('F j, Y @ g:i A')不会导致错误,但会返回null.
chi*_*t24 12
将以下代码添加到我的模型中对我有用:
public function getCreatedAtAttribute($date)
{
if(Auth::check())
return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->copy()->tz(Auth::user()->timezone)->format('F j, Y @ g:i A');
else
return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->copy()->tz('America/Toronto')->format('F j, Y @ g:i A');
}
public function getUpdatedAtAttribute($date)
{
return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('F j, Y @ g:i A');
}
Run Code Online (Sandbox Code Playgroud)
这让我使用created_at,并updated_at在我想要的格式.
我确实遇到了同样的问题,在我寻找答案的过程中,我如何解释一个新的\ DateTime('0000-00-0000:00 00:00')的结果?.
我决定将数据库中的datetime-columns更改为nullable,默认值为NULL,以防止字段具有值'0000-00-00 00:00:00'.
我在laravel 5中的迁移看起来像这样:
Schema::table('table', function($table)
{
$table->dateTime('created_at')->nullable()->default(null)->change();
$table->dateTime('updated_at')->nullable()->default(null)->change();
});
Run Code Online (Sandbox Code Playgroud)