Tags: PHP
Laravel's container does not instantiate type hinted model attributes in custom request classes, when accessed from testing environment. To bypass this, bind the instance to the app inside the test and force instantiate it in the request constructor.
In CustomRequest:
private $user;
public function __construct(User $user)
{
$this->user = $user;
}
In the test's SetUp method:
$this->user = factory(\App\Models\User::class)->create();
$this->app->instance(\App\Models\User::class, $this->user);
©2023, kirillsimin.com