Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get settings #85

Closed
maganius opened this issue Aug 23, 2021 Discussed in #83 · 2 comments
Closed

Can't get settings #85

maganius opened this issue Aug 23, 2021 Discussed in #83 · 2 comments

Comments

@maganius
Copy link

maganius commented Aug 23, 2021

This does not work for me and I do not know why

app (GeneralSetting::class)

it return

App\Settings\GeneralSetting {# 653 ▼
   -mapper: Spatie\LaravelSettings\SettingsMapper {# 655 ▶}
   -config: Spatie\ LaravelSettings\SettingsConfig {# 654 ▶}
   -loaded: false
   -configInitialized: true
   #originalValues: null
}

Injecting the settings in a controller works correctly.

@rubenvanassche
Copy link
Member

This is the default behavior, settings only get loaded from the repository when a property is loaded like this:

app(GeneralSetting::class)->site_name

Or when you call toArray on it:

resolve(GeneralSetting::class)->toArray()

@lucianobosco
Copy link

This is the default behavior, settings only get loaded from the repository when a property is loaded like this:

app(GeneralSetting::class)->site_name

Or when you call toArray on it:

resolve(GeneralSetting::class)->toArray()

Well, I was pulling my hair out trying to figure why using a \Log::debug($settings->site_name) was the only solution to make this work.

For those using this in a Service Class, my approach was doing this

<?php

namespace App\Services;

use GeneralSettings;

class MyService {

    protected $generalSettings;

    /**
     * Constructs a new service object.
     *
     */
    public function __construct()
    {
        $this->generalSettings = resolve(GeneralSettings::class)->toArray();
    }
?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants