skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. Because $this->app refers to the IlluminateSupportServiceProvider contract. But the method isProduction() does not exist there.

    Laravel resolves this to the IlluminateFoundationApplication class, which does have the isProduction() method.

    So what you can do, is type-hint a variable, something like:

        public function boot(): void
        {
            /** @var IlluminateFoundationApplication $app */
            $app = $this->app;
    
            Model::preventLazyLoading(!$app->isProduction());
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search