使用 Laravel 5 和 Carbon 显示今天的文章
Show todays article using Laravel 5 and Carbon
我正在尝试使用 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);
}
为了检查时间戳是否来自今天,DATE()
函数派上用场:
$today = Carbon::now()->toDateString();
$article = Article::whereRaw('DATE(published_at) = ?', [$today])->first();
我正在尝试使用 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);
}
为了检查时间戳是否来自今天,DATE()
函数派上用场:
$today = Carbon::now()->toDateString();
$article = Article::whereRaw('DATE(published_at) = ?', [$today])->first();