php Laravel 中的 Carbon 4 InvalidArgumentException - 发现意外数据。尾随数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24168470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Carbon in Laravel 4 InvalidArgumentException - Unexpected data found. Trailing data
提问by eComEvo
I'm trying to get an Eloquent query result for DB::raw("DATE_FORMAT(created_at, '%m-%d-%Y %r') AS created_at")but each time I get this exception from Carbon:
我正在尝试获取 Eloquent 查询结果,DB::raw("DATE_FORMAT(created_at, '%m-%d-%Y %r') AS created_at")但每次我从 Carbon 收到此异常时:
InvalidArgumentException
Unexpected data found. Trailing data
If I change it to just created_atinstead of employing MySQL's DATE_FORMAT()function, then it gets the data without issue.
如果我将其更改为仅created_at使用 MySQL 的DATE_FORMAT()功能而不是使用它,则它可以毫无问题地获取数据。
I've not only done this sort of date formatting without issue before, but I checked every field in the database table (there are only 10 for this seed) and each is a standard valid date, so I'm wondering why Carbon is pitching a fit.
我之前不仅没有问题地完成了这种日期格式,而且我检查了数据库表中的每个字段(这个种子只有 10 个)并且每个字段都是一个标准的有效日期,所以我想知道为什么 Carbon 会投球适合。
Running this in Laravel 4.1.
在 Laravel 4.1 中运行它。
回答by The Alpha
In an Eloquentquery result (model) every datefield is a carbon object, it means, if you query a model which contains any timestampfield like created_at, updated_at(basically created using timestamps()during migration) and deleted_at, Laravelconverts them to a Carbonobject and you may use any public methods of Carbon, for example:
在Eloquent查询结果(模型)中,每个date字段都是一个碳对象,这意味着,如果您查询包含任何timestamp字段的模型,例如created_at, updated_at(基本上是timestamps()在迁移期间使用创建的) and deleted_at,Laravel将它们转换为Carbon对象,并且您可以使用任何公共方法Carbon, 例如:
$user = User::find(1);
// '2014-04-20 19:02:09' will become 'Apr 20, 2014'
$user->created_at->toFormattedDateString();
So, you may directly use any public method of Carbonon a timestampfield available in a model. If you try this:
所以,你可以直接使用的任何公开的方法Carbon在timestamp模型中的可用字段。如果你试试这个:
dd($user->created_at);
Then the output will be:
然后输出将是:
object(Carbon\Carbon)[456]
public 'date' => string '2014-04-20 19:02:09' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'UTC' (length=3)
So, if you want to formata date, you may use:
所以,如果你想format约会,你可以使用:
// outputs like: 'Sunday 20th of April 2014 07:02:09 PM'
$user->created_at->format('l jS \of F Y h:i:s A')
Update:
更新:
If you want to change this behavior, means that, if you want tell Laravelthat, which fields should be converted automatically to Carbonobject then you may override that by creating a method in your model like:
如果您想更改此行为,则意味着,如果您想说明Laravel哪些字段应自动转换为Carbon对象,那么您可以通过在模型中创建一个方法来覆盖它,例如:
public function getDates()
{
// only this field will be converted to Carbon
return array('updated_at');
}
To totally disable date mutations, simply return an empty array from the getDatesmethod. For more details, check Date Mutatorson Laravelwebsite.
要完全禁用日期更改,只需从该getDates方法返回一个空数组。有关更多详细信息,请查看网站上的Date MutatorsLaravel。
回答by voidstate
I realise the original question refers to MySQL but I had the same error with MSSQL. The problem turned out to be that MSSQL's datetime column type has a precision of .001 seconds but I was setting my model's format to no precision:
我意识到原来的问题是指 MySQL,但我在使用 MSSQL 时遇到了同样的错误。问题原来是 MSSQL 的日期时间列类型的精度为 0.001 秒,但我将模型的格式设置为无精度:
protected function getDateFormat()
{
return 'Y-m-d G:i:s';
}
By using the newer DateTime2 column type and turning off the precision, I fixed the error. I.e.
通过使用较新的 DateTime2 列类型并关闭精度,我修复了错误。IE
datetime2(0)
You could instead change the format in getDateFormat, of course.
getDateFormat当然,您可以改为更改 中的格式。
回答by Wireblue
If it helps anyone else, I got the same error when attempting to copy a date.
如果它对其他人有帮助,那么在尝试复制日期时我会遇到同样的错误。
$user->last_login = Carbon::now();
if ($user->first_login < Carbon::createFromDate(2000, 1, 1)) {
// This is the users first login
$user->first_login = $user->last_login; // FAILS!
}
Turns out Laravel casts the value of $user->last_loginto a DateTime+Timezone string. It's no longer a Carbon object.
结果 Laravel 将 的值$user->last_login转换为 DateTime+Timezone 字符串。它不再是一个碳对象。
You could fix the error by using copies of a single Carbon object (example below), or by setting up mutators (setters) on the underlying model.
您可以通过使用单个 Carbon 对象的副本(下面的示例)或通过在底层模型上设置修改器(setter)来修复错误。
$now = Carbon::now();
$user->last_login = $now;
if ($user->first_login < Carbon::createFromDate(2000, 1, 1)) {
// This is the users first login
$user->first_login = $now;
}

