-
app/Helpers/date.php
Open in GitHubuse Illuminate\Support\Carbon; function carbon(string $parseString = '', string $tz = null): Carbon { return new Carbon($parseString, $tz); }
-
app/Models/User.php
Open in GitHubuse Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements MustVerifyEmail { public function scopeLastWeek(Builder $query): Builder { return $query->whereBetween('registered_at', [carbon('1 week ago'), now()]) ->latest(); } }
-
tests/Unit/PostTest.php
Open in GitHubuse App\Models\Post; use Tests\TestCase; class PostTest extends TestCase { public function testGettingOnlyLastWeekPosts() { Post::factory() ->count(3) ->create() ->each(function ($post) use ($faker) { $post->posted_at = $faker->dateTimeBetween(carbon('3 months ago'), carbon('2 months ago')); $post->save(); }); Post::factory() ->count(3) ->create() ->each(function ($post) use ($faker) { $post->posted_at = $faker->dateTimeBetween(carbon('1 week ago'), now()); $post->save(); }); } }
-
composer.json
Open in GitHub{ "name": "guillaumebriday/laravel-blog", "require": { "php": "^7.4", "barryvdh/laravel-debugbar": "3.5.1", "doctrine/dbal": "2.11.1", // ... }, "autoload": { "files": [ "app/Helpers/date.php" ] }, // ... other settings }