skip to Main Content

Laravel sometimes error 500, SQLSTATE[HY000] [1045] I’m trying to make application settings from the database, otherwise load from .env
How to make a request to the database in the service provider?
Sometimes there is no error, sometimes there is error 500 and there is no connection to the database.

AppServiceProvider.php

    public function boot(): void
    {
        Schema::defaultStringLength(191);
        Vite::useScriptTagAttributes(['async' => true]);
        Blade::anonymousComponentPath(__DIR__.'/../../resources/views/admin/layout/components', 'admin');
        Blade::anonymousComponentPath(__DIR__.'/../../resources/views/template/layout/components', 'template');

        $namesVariables = [
            'name',
            'description',
            'lang',
            'timezone',
            'telegram',
            'url',
            'admin_url',
        ];

        foreach($namesVariables as $variable)
        {
            ${'site_'.$variable} = Cache::get('site_'.$variable, '');
        }

        if (empty($site_name) && empty($site_description)
                              && empty($site_lang)
                              && empty($site_timezone)
                              && empty($site_telegram)
                              && empty($site_url)
                              && empty($site_admin_url))
        {
            $settings = Setting::find(1);

            if (!empty($settings))
            {
                foreach($namesVariables as $variable)
                {
                    ${'site_'.$variable} = $settings['site_'.$variable];
                    Cache::put('site_'.$variable, ${'site_'.$variable},
                           now()->addMinutes($this->cacheNum));
                }
            } else {
                $site_name = env('APP_NAME');
                $site_description = env('APP_DESCRIPTION');
                $site_telegram = env('APP_TELEGRAM');
                $site_url = env('APP_URL');
                $site_admin_url = env('APP_ADMIN_URL');

                Cache::put('site_name', $site_name,
                       now()->addMinutes($this->cacheNum));
                Cache::put('site_description', $site_description,
                       now()->addMinutes($this->cacheNum));
                Cache::put('site_telegram', $site_telegram,
                       now()->addMinutes($this->cacheNum));
                Cache::put('site_url', $site_url,
                       now()->addMinutes($this->cacheNum));
                Cache::put('site_admin_url', $site_admin_url,
                       now()->addMinutes($this->cacheNum));
            }
        }

        Config::set('app.name', $site_name);
        Config::set('app.site_description', $site_description);
        App::setLocale($site_lang);
        Config::set('app.timezone', $site_timezone);
        Config::set('app.site_telegram', $site_telegram);
        Config::set('app.url', $site_url);
        Config::set('app.site_admin_url', $site_admin_url);
    }

Log
Laravel log gives an error

[2024-08-03 09:40:00] production.ERROR: SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: YES) (Connection: mysql, SQL: select * from `settings` where `settings`.`id` = 1 limit 1) {"exception":"[object] (Illuminate\Database\QueryException(code: 1045): SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: YES) (Connection: mysql, SQL: select * from `settings` where `settings`.`id` = 1 limit 1) at D:\Web\laragon\www\mysite\vendor\laravel\framework\src\Illuminate\Database\Connection.php:829)
[stacktrace]
#0 

$settings = Setting::find(1);

2

Answers


  1. Chosen as BEST ANSWER

    "If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application."

    Example: $site_name = env('APP_NAME'); Replace: $site_name = config('app.name');

    Problem in: Don't use env() in service providers replace on config()


  2. You get a privilege error from MySQL. You will need to look at the

    'forge'@'localhost'
    

    user and make sure that the credentials you pass are correct. Try connecting directly to your RDBMS using the credentials you see in the .env. If it fails, then contact your administrator and ask for privileges or a new password. If it succeeded, then the user you are testing with simply does not have the necessary permissions to run the query on the database and/or table. In those cases you (or the administrator) will need to look into the privileges of this user and grant the necessary privileges for this query to be executed.

    Let’s put aside Laravel at this point and let’s connect directly to MySQL and see how that goes. When that’s succeeding, then update your env at Laravel and test again. Once your MySQL connection will work without Laravel, but will not work from Laravel, then you will need to look into problems with Laravel. But not yet. First make sure that the service Laravel is to use is properly working.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search