使用Laravel 5和Carbon显示今天的文章

时间:2015-03-01 19:17:58

标签: laravel-5 php-carbon

我正在尝试使用Laravel 5和carbon来展示每日文章。我已将published_at字符串转换为碳对象,因此我可以获得比较的日期。我只是无法弄清楚where子句会是什么。

//this is in my model to make it a carbon object
protected $dates = ['published_at'];

//method in my controller
public function current()
{

    $article = Article::where('published_at', '>=', Carbon::now())->first();

    //testing prints 2 but today is the 1st
    dd($article->published_at->day);

    return view('articles.show')->with('article', $article);
}

1 个答案:

答案 0 :(得分:2)

为了检查时间戳是否来自今天,DATE()功能派上用场:

$today = Carbon::now()->toDateString();
$article = Article::whereRaw('DATE(published_at) = ?', [$today])->first();