[英]Laravel date not allowing me to use diffForHumans
have my table set up with a timestamp fields for created and updated at fields.让我的表设置一个时间戳字段,用于在字段中创建和更新。
In my model I then do this:在我的模型中,我会这样做:
protected $dates = ['created_at', 'updated_at'];
But when calling the date:但是当调用日期时:
$p->created_at->diffForHumans()
I get我得到
Call to a member function diffForHumans() on string
I'm pretty sure that should work.我很确定这应该有效。 I have used the same many times before on different models etc but this just won't work.
我以前在不同的模型等上使用过很多次,但这不起作用。
By default, Eloquent will convert the created_at and updated_at columns to instances of Carbon, which provides an assortment of helpful methods, and extends the native PHP DateTime class.默认情况下,Eloquent 会将 created_at 和 updated_at 列转换为 Carbon 的实例,它提供了一系列有用的方法,并扩展了原生 PHP DateTime 类。
Read: https://laravel.com/docs/5.1/eloquent-mutators#date-mutators阅读: https : //laravel.com/docs/5.1/eloquent-mutators#date-mutators
Just check your composer.json file for nesbot/carbon package.只需检查您的 composer.json 文件中的 nesbot/carbon 包。 In case if you don't have you can install it by typing
如果你没有,你可以通过输入安装它
composer require nesbot/carbon
Alternative Solution is,替代解决方案是,
You could use Carbon::parse() to create object on the fly.您可以使用 Carbon::parse() 动态创建对象。
Carbon::parse($p->created_at)->diffForHumans();
The Laravel diffForHumans()
functions as I have used in version 5.2, only works when you are using on created_at
and updated_at
, otherwise it will bring you the Call to a member function diffForHumans() on string
error.我在 5.2 版中使用的 Laravel
diffForHumans()
函数仅在您在created_at
和updated_at
上使用时才有效,否则它会Call to a member function diffForHumans() on string
错误时Call to a member function diffForHumans() on string
。 even though you have used the created_at
and updated_at
, just be sure that on your migrations, you haven't used:即使您使用了
created_at
和updated_at
,请确保在您的迁移中,您没有使用:
$table->timestamp('created_at');
$table->timestamp('updated_at');
But you have used但是你用过
$table->timestamps();
There is a difference according to laravel But just incase for some reason you wanted to use the diffForHumans()
function for a column that is neither created_at
and updated_at
, For instance expired_at
, Use Carbon根据 laravel 存在差异但只是出于某种原因,您想对既不是
created_at
又不是updated_at
的列使用diffForHumans()
函数,例如expired_at
,使用 Carbon
What I can say in summary is that:我能总结的就是:
Addon
in your laravel project folder eg test-laravelAddon
的新文件夹,例如 test-laravelCarbon.php
.Carbon.php
的新文件。Carbon.php
file, enter:Carbon.php
文件中,输入:namespace Carbon;
class Carbon extends \DateTime{
//
}
Then go to your controller eg ProductController.php
and add the Carbon namespace to the file:然后转到您的控制器,例如
ProductController.php
并将 Carbon 命名空间添加到文件中:
namespace Addon;
namespace App\Http\Controllers;
use Carbon\Carbon;
use ...
class ProductController extends Controller{
$expire_date_string = '2016-07-27 12:45:32';
// Parse date with carbon
$carbonated_date = Carbon::parse($expire_date_string);
// Assuming today was 2016-07-27 12:45:32
$diff_date = $carbonated_date->diffForHumans(Carbon::now());
echo $diff_date; // prints "1 month after"
}
For more information about the Carbon Controllers, visit: http://carbon.nesbot.com/docs/有关碳控制器的更多信息,请访问: http : //carbon.nesbot.com/docs/
You don't need to use Carbon for a column that is neither created_at and updated_at just add the new column like expired_at to your model:您不需要将 Carbon 用于既不是 created_at 又不是 updated_at 的列,只需将像 expired_at 这样的新列添加到您的模型中即可:
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'created_at',
'updated_at',
'deleted_at',
'expired_at'
];
And then you can use ->diffForHumans();然后你可以使用 ->diffForHumans();
https://laravel.com/docs/5.3/eloquent-mutators#date-mutatorshttps://laravel.com/docs/5.3/eloquent-mutators#date-mutators
您必须使用Carbon
,如下所示:
$date = new \Carbon($p->created_at)->diffForHumans();
如果在刀片视图中:
{{ \Carbon\Carbon::parse($post->created_at)->diffForHumans() }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.