You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm increasing the safety of my application by using the Model::shouldBeStrict() method in my AppServiceProvider.
That's working great, however specifically for the preventLazyLoading part, I'd like to handle that automatically in production, so no errors are thrown for this specific problem. This is what I have:
// Everything strict, all the time.
Model::shouldBeStrict();
// In production, merely log lazy loading violations.if ($this->app->isProduction()) {
Model::handleLazyLoadingViolationUsing(function (Model$model, string$relation) {
$class = get_class($model);
info("Attempted to lazy load [{$relation}] on model [{$class}].");
});
}
I added the following test to make sure this is working:
test('N+1 queries are not allowed', function () {
User::factory()->count(2)->create();
$output = [];
$users = User::all();
foreach ($usersas$user) {
// `address` is lazy loaded.$output[] = $user->name.' - '.$user->address->street_address_line_1;
}
})->throws(LazyLoadingViolationException::class);
So far so good, however I'm facing problems when trying to assert that it does allow it in production. I have the following test:
test('N+1 queries are allowed in production', function () {
config(['app.env' => 'production']);
User::factory()->count(2)->create();
$output = [];
$users = User::all();
foreach ($usersas$user) {
// `address` is lazy loaded.$output[] = $user->name.' - '.$user->address->street_address_line_1;
}
})->throwsNoExceptions();
However it doesn't work. It throws the exception anyways.
Just to debug it, I tried to add the following log to the AppServiceProvider:
// Everything strict, all the time.
Model::shouldBeStrict();
logger('Logging inside AppServiceProvider. isProduction = '.($this->app->isProduction() ? 'true' : 'false'));
// In production, merely log lazy loading violations.if ($this->app->isProduction()) {
Model::handleLazyLoadingViolationUsing(function (Model$model, string$relation) {
$class = get_class($model);
info("Attempted to lazy load [{$relation}] on model [{$class}].");
});
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello there, greetings!
I'm increasing the safety of my application by using the
Model::shouldBeStrict()
method in my AppServiceProvider.That's working great, however specifically for the
preventLazyLoading
part, I'd like to handle that automatically in production, so no errors are thrown for this specific problem. This is what I have:I added the following test to make sure this is working:
So far so good, however I'm facing problems when trying to assert that it does allow it in production. I have the following test:
However it doesn't work. It throws the exception anyways.
Just to debug it, I tried to add the following log to the AppServiceProvider:
It always log that it's
false
.What is the correct way to test this?
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions