Retrieve records older than a specified amount of time in Laravel

You can use the Carbon library to easily create an eloquent query that will filter the records based on a given amount of elapsed time.

Let’s see an example where clause which gives us the records that are at most 4 weeks old:

<?php

Messages::where('created_by', '>=', Carbon::now()->subWeeks(4)->toDateTimeString());

This example was tested in Laravel 5.2.