3

I have a table: settings with the model Setting

class Setting extends Model
{
    protected $fillable = [
        'name', 'value',
    ];
}

I have created a service provide SettingsServiceProvider and registered in app.php

class SettingsServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot(Factory $cache, Setting $settings)
    {
        if (\Schema::hasTable('settings')) {
            config()->set('settings', Setting::pluck('value', 'name')->all());
        }
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {

    }
}

After adding the name and value to the table settings, I am calling it in the view like this:

{{ config('settings.sitename') }}

Where sitename is the name field which returns the value perfectly.

Problem: The problem with this method is that with every page request makes a DB call. And as these settings are not meant to be changed frequently, so I was looking for a method to cache it in the laravel cache.

I tried the following:

class SettingsServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot(Factory $cache, Setting $settings)
    {
        $settings = $cache->remember('settings', 60, function() use ($settings)
        {
        return $settings->pluck('name', 'value')->all();
        });
        config()->set('settings', $settings);

        /* if (\Schema::hasTable('settings')) {
            config()->set('settings', Setting::pluck('value', 'name')->all());
        } */
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

But when I try to return it to the view like this:

{{ config('settings.sitename') }}

nothing gets return.

Saurabh
  • 2,655
  • 1
  • 20
  • 47
  • 1
    As `remember` is a way of getting a value - with a default if it doesn't exist, it could be that a value already exists from earlier testing (for example). It could be worth restarting the server (if possible) and see if it still happens. – Nigel Ren May 03 '17 at 07:03
  • 1
    I tried everything, it doesn't returns any value in the view. – Saurabh May 03 '17 at 07:05
  • 1
    Have you tried clearing the config cache `php artisan config:clear ` – linktoahref May 03 '17 at 07:10
  • 1
    Thank you for your suggestion. I haven't tried it before, I tried it just now but still no value was returned to the view. – Saurabh May 03 '17 at 07:13
  • 1
    What do you get when you do `{{ dd(config('settings')) }}` ? – linktoahref May 03 '17 at 07:15
  • 1
    It returns an array with 8 elements, But the settings table has 12 columns. – Saurabh May 03 '17 at 07:21

2 Answers2

4

I don't have enough reputation to comment on the post but this should solve it

Replace:

return $settings->pluck('name', 'value')->all();

With:

return $settings->pluck('value', 'name')->all();

and then with the usual:

php artisan config:clear
php artisan cache:clear
Mozammil
  • 8,520
  • 15
  • 29
0

In command prompt, just type

php artisan 

OR

 php artisan list

It gives list of command with Explanation which help u lot

enter image description here

Thank you.

Brijesh Dubey
  • 118
  • 2
  • 12