Php 使用Carbon进行Laravel查询

Php 使用Carbon进行Laravel查询,php,facebook,laravel,laravel-4,php-carbon,Php,Facebook,Laravel,Laravel 4,Php Carbon,我如何使用碳来输出在上创建的人类可读的查询,比如Facebook在一分钟前发布的。问题是我总是将查询结果直接返回到浏览器中输出。我如何整合它,而不是2015-07-01 12:32:43它将是1分钟前的或其他人类可读的好东西 在我的控制器中: $posts = DB::table('posts') ->where('author_id', '=', $id) ->where('created_at', '<

我如何使用碳来输出在上创建的人类可读的查询,比如Facebook在一分钟前发布的。问题是我总是将查询结果直接返回到浏览器中输出。我如何整合它,而不是
2015-07-01 12:32:43
它将是
1分钟前的
或其他人类可读的好东西

在我的控制器中:

    $posts = DB::table('posts')
                ->where('author_id', '=', $id)
                ->where('created_at', '<', $date)
                ->orderBy('created_at', 'desc')
                ->paginate(10);

    return $posts;
$posts=DB::table('posts'))
->其中('author_id','=',$id)
->其中('created_at','尝试:


在返回之前,我建议您使用雄辩的模型及其附带的功能:

class Post extends Eloquent {
    protected $appends = array('created_at_for_humans');

    public function getCreatedAtForHumansAttribute(){
        return Carbon::parse($this->attributes['created_at'])->diffForHumans();
    }
}
这将返回格式化的日期,因为它是在
$appends
中注册的,所以该属性将包含在array/JSON转换中

您的查询看起来几乎相同:

$posts = Post::where('author_id', '=', $id)
             ->where('created_at', '<', $date)
             ->orderBy('created_at', 'desc')
             ->paginate(10);
$posts=Post::where('author_id','=',$id)

->where('created_at','谢谢。它正在工作。我只需要删除您的
$post
.Typo中的
&
$posts = Post::where('author_id', '=', $id)
             ->where('created_at', '<', $date)
             ->orderBy('created_at', 'desc')
             ->paginate(10);