“isProduction”方法未在”Laravel”中的”$this->app->isProduction()”中检测到。

huangapple go评论62阅读模式
英文:

isProduction method is not detected in "$this->app->isProduction()" in laravel

问题

我目前正在面对一个非常奇怪的 bug,我试图在 Laravel 项目中添加 preventLazyLoading 限制。但问题是,我按照 Laravel 文档上关于如何配置 Laravel 严格性并实现限制的步骤进行了操作。而且,我甚至运行了 "php artisan config:clear" 命令以确保配置生效。但问题是,当我使用条件默认全局方法 "$this->app->isProduction()" 时,Laravel 无法检测到 isProduction() 方法。我以前从未遇到过这种情况。如果您有任何解决问题的想法,请在下面分享建议。非常感谢您阅读本论坛。

app\Providers\AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\ServiceProvider;

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());
    }
}

(注意:这是你提供的代码的翻译部分,没有其他内容。)

英文:

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.

app\Providers\AppServiceProvider.php

&lt;?php

namespace App\Providers;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\ServiceProvider;

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-&gt;app-&gt;isProduction());
    }
}

答案1

得分: 2

Because $this->app refers to the \Illuminate\Support\ServiceProvider contract. But the method isProduction() does not exist there.

Laravel resolves this to the \Illuminate\Foundation\Application class, which does have the isProduction() method.

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

    public function boot(): void
    {
        /** @var \Illuminate\Foundation\Application $app */
        $app = $this->app;

        Model::preventLazyLoading(!$app->isProduction());
    }
英文:

Because $this-&gt;app refers to the \Illuminate\Support\ServiceProvider contract. But the method isProduction() does not exist there.

Laravel resolves this to the \Illuminate\Foundation\Application class, which does have the isProduction() method.

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

    public function boot(): void
    {
        /** @var \Illuminate\Foundation\Application $app */
        $app = $this-&gt;app;

        Model::preventLazyLoading(!$app-&gt;isProduction());
    }

答案2

得分: 0

抱歉给大家添麻烦!似乎只是 VS Code IDE 报告未定义并突出显示它。但项目似乎没有任何问题。限制也有效。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月8日 18:11:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定