ホーム > Laravel > How to Retrieve Data Only from Yesterday in Laravel
Laravel

How to Retrieve Data Only from Yesterday in Laravel

Thank you for your continued support.
This article contains advertisements that help fund our operations.

Table Of Contents

  1. Implementation
  2. Conclusion

⇨ Click here for the table of contents for Laravel articles

This article discusses how to retrieve data from yesterday in Laravel.

Implementation

By specifying the date (in the format like 2021-08-09) with WhereDate, you can implement this.

PostController

//Using Carbon
use Carbon\Carbon;
//Using the model
use App\Models\User;

class HomeController extends Controller
{
  public function index()
  {
    //Output yesterday in the appropriate format using Carbon
    $yesterday = Carbon::now()->subDay()->format('Y-m-d');
    //Using whereDate
    $users = User::whereDate('created_at', $yesterday)->get();
    dd($users);
  }
}

Conclusion

As shown above, by simply changing the date, it is possible to retrieve data specified for a specific date.

Popular Articles

Deploying a PHP 7.4 + Laravel 6 Project to AWS EC2

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!