Laravel 日期不允许我使用 diffForHumans
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34278455/
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
Laravel date not allowing me to use diffForHumans
提问by Lovelock
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 Roshimon
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. In case if you don't have you can install it by typing
只需检查您的 composer.json 文件中的 nesbot/carbon 包。如果你没有,你可以通过输入安装它
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();
回答by Pyr James
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. even though you have used the created_at
and updated_at
, just be sure that on your migrations, you haven't used:
Laravel 的diffForHumans()
功能和我在 5.2 版本中使用的一样,只有当你使用created_at
and时才有效updated_at
,否则会给你带来Call to a member function diffForHumans() on string
错误。即使您使用了created_at
and 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 instanceexpired_at
, Use Carbon
有根据laravel但只是柜面出于某种原因,你想用的不同diffForHumans()
功能列,既不created_at
和updated_at
,例如expired_at
,用碳
What I can say in summary is that:
我能总结的就是:
- You first create a new folder may be called
Addon
in your laravel project folder e.g test-laravel - Then inside the folder, you create a new file called
Carbon.php
. - Inside the
Carbon.php
file, enter:
- 您首先创建一个新文件夹,可能会
Addon
在您的 Laravel 项目文件夹中调用,例如 test-laravel - 然后在文件夹内,创建一个名为
Carbon.php
. - 在
Carbon.php
文件中,输入:
namespace Carbon;
class Carbon extends \DateTime{
//
}
Then go to your controller e.g 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/
回答by Francisco Fernandes
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-mutators
https://laravel.com/docs/5.3/eloquent-mutators#date-mutators
回答by Tumelo Mapheto
If in blade view:
如果在刀片视图中:
{{ \Carbon\Carbon::parse($post->created_at)->diffForHumans() }}
回答by Alex
You have to use Carbon
, like this:
你必须使用Carbon
,像这样:
$date = new \Carbon($p->created_at)->diffForHumans();