I am currently facing a really odd bug where I am trying to add preventLazyLoading restriction in a laravel project. But the thing is I followed the laravel docs on how to configure laravel strictness and implemented the restriction. And, I even ran "php artisan config:clear" command to make sure the configuration is taking in effect. But the thing is when I use the conditional default global method of "$this->app->isProduction()" the laravel does not detect the isProduction() method. I have never encountered it before. Please if you have any idea on how to solve the problem, share the suggestion below. I really appreciate you for reading this forum.
appProvidersAppServiceProvider.php
<?php
namespace AppProviders;
use IlluminateDatabaseEloquentModel;
use IlluminatePaginationPaginator;
use IlluminateSupportServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Model::preventLazyLoading(! $this->app->isProduction());
}
}
2
Answers
Sorry for the trouble guys! It seem to be just VS Code IDE saying undefined and highlighting it. But the project does not seem to have any problem. The restriction also works too.
Because
$this->app
refers to theIlluminateSupportServiceProvider
contract. But the methodisProduction()
does not exist there.Laravel resolves this to the
IlluminateFoundationApplication
class, which does have theisProduction()
method.So what you can do, is type-hint a variable, something like: