Search code examples
laravel-5php-carbon

Show todays article using Laravel 5 and Carbon


I'm trying to show a daily article using Laravel 5 and carbon. I have converted the published_at string to a carbon object so I can get just the day to compare. I just can't figure out the where clause would be.

//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);
}

Solution

  • For checking if a timestamp is from today, the DATE() function comes in handy:

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