0

我正在尝试使用 Laravel 5 和碳展示一篇每日文章。我已将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);
}
4

1 回答 1

2

为了检查时间戳是否从今天开始,该DATE()函数派上用场:

$today = Carbon::now()->toDateString();
$article = Article::whereRaw('DATE(published_at) = ?', [$today])->first();
于 2015-03-01T19:40:28.710 回答