Data available on every page

Posted 7 months ago by Zenith2012

Hi,

I currently have a migration as follows:

public function up()
    {
        Schema::create('settings', function (Blueprint $table) {
            $table->increments('id');
            $table->string('slug');
            $table->string('category');
            $table->string('name');
            $table->string('value');
            $table->timestamps();
        });

        DB::table('settings')->insert([
            'slug'      => 'app.version',
            'category'  => 'APP',
            'name'      => 'VERSION',
            'value'     => '0.1.0',
            'created_at' => Now(),
            'updated_at' => Now(),
        ]);
    }

As you can see this creates an entry for app.version, I have a subsequent Setting model so I can do $settings = \App\Setting::all() then process these however at the moment I'm doing this in every method in every view... e.g.

public function index()
    {
        $settings = Cache::remember('settings', 60, function()
        {
            $tmp = \App\Setting::all();
            $settings = array();
            foreach ($tmp as $s) {
                $settings[$s['slug']] = $s;
            }
            return $settings;
        });

        return view('clients.index', [
            'settings' => $settings
        ]);
    }

However, I'm sure there's a better way to do this? Essentially I want to be able to access the $settings data (which should be cached) on any page.

Any and all suggestions greatly appreciated.

Please sign in or create an account to participate in this conversation.