3

I have the following code in my AppServiceProvider.php file:

<?php

namespace App\Providers;

use App\Models\Setting;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Schema;
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()
    {        
        if (Schema::hasTable('settings')) {
            foreach (Setting::all() as $setting) {
                Config::set('settings.'.$setting->key, $setting->value);
            }
        }
    }
}

Which does it's job fine locally, but when I deploy via DeployHQ, it kills the process with the following error:

SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = giga and table_name = settings and table_type = 'BASE TABLE')

Which kinda makes sense, the database doesn't exist on the build server, so the check cannot run as there's nothing to check. Is there a different way to hydrate a settings config with values from a database on boot which doesn't affect the running of php artisan package:discover?

I know it'll probably be asked, but the .env file etc is all set up correctly. This issue is to do with the fact the build server doesn't have the database, but the server the files get piped to does.

Edit: To give some more context, and perhaps some advice could be given on this, I'm only really using this config value in this code inside a Service class:

public function __construct()
{
    $this->domain = config('api.domain');
    $this->apiVersion = config('api.version');
    $this->bearerToken = config('settings.bearer_token');
    $this->clientId = config('api.client_id');
    $this->clientSecret = config('api.client_secret');
}

Everything online suggests putting these values into the config, however if it's only being called here would it be okay to retrieve it from the Database directly?

Andy Holmes
  • 7,817
  • 10
  • 50
  • 83
  • Why not run Composer without the script section? – Nico Haase Jan 26 '23 at 18:39
  • 1
    Just a side not, which to me seems to be more important. You do `realize` that the `AppServiceProvider` get's booted just more than once? As in every time the application needs to respond? I would definitely go for an approach to generate a config based on settings from a table where in the end this config can be cached. Why wouldn't an Artisan command do the trick? This could also be triggered somewhere in your deploy/build process. – dbf Jan 26 '23 at 19:01
  • @NicoHaase Because auto discovery is important for some of the packages being installed. – Andy Holmes Jan 26 '23 at 19:03
  • @dbf I do, yeah, it's not ideal but it fixed an initial problem I had, however I'm at a refactoring point (especially with this error) so looking to improve it all anyway. Are you saying just make an artisan command that touches a config file and pulls the values from the DB into it? the bearer token updates every night at midnight currently, so I guess I'd just run this alongside it – Andy Holmes Jan 26 '23 at 19:05
  • 1
    Yes, you can save the config each time it changes, e.g. job triggered by a `TokenChangedEvent` which saves the config with a new token, then rebuild/recache the config. I don't know the order of things, e.g. why storing the config in booted state solved your problem. But in projects with more comprehensible deploy/build methods, I got used to an environment I've build which executes a script (python or bash) which again would trigger artisan prepare/build/deploy commands just for this purpose. Some file with a hashed key would then be present where the app knew it could boot. – dbf Jan 26 '23 at 19:21
  • 1
    I would definitely do this kind of work or processing **without** the need of Laravel to avoid circular failures like this. Just some thoughts getting a deja-vu while I was reading your question. – dbf Jan 26 '23 at 19:23
  • @dbf that's super helpful, thanks very much. I'll start looking at this tomorrow morning :) – Andy Holmes Jan 26 '23 at 20:07

0 Answers0