🤹
TALL Stack Tips
  • What is TALL Stack
  • Tailwind
    • Tailwind Resources
    • Swinging Bell Notification Icon
    • Styled Unordered Lists
  • Alpine
  • Alpine Resources
  • Tabbed Content Using Alpine JS
  • Checkbox component with SVG tick
  • Dropdown animation
  • Create a Sliding Puzzle Captcha
  • Laravel
    • Tabler Icons Component
    • Password-less Login with Laravel 8+
    • Password-less Login with Magic Link in Laravel 8
    • Laravel Resources
    • Laravel Breeze Login Conditional Redirect
    • Jetstream Login Conditional Redirect
    • Simplify Laravel CRUD Controllers
    • CSRF and expired login forms
    • CSRF and expired logout forms
    • Add your own logo to Laravel Mail
    • Specify a different mail theme for Notifications
    • Show custom page when email verification link expired
    • Using a mutator to save currency
    • Using Spatie Valuestore to hold frequently accessed settings
    • Using the old() helper
    • Alternatives to using Eloquent Accessor
    • UpdateOrCreate may not update timestamp
    • Use of lockForUpdate()
    • Using S3
    • Super Simple User Based Feature Flags
    • Installing a Specific Version of Laravel
    • Versioning your Laravel Project
    • CSS Cache Busting with your Git Commit SHA
    • Adding column to Database Notifications table
    • Find nearby locations using the Haversine formula in Eloquent query
    • Using Queues on Shared Hosting with Laravel
    • Create Guaranteed Unique Invoice Number in Laravel
    • Send Notification to all team members
    • Protect Staging site with Basic Auth
    • Working with Enums
    • PHP DateTime formatting cribsheet
  • Livewire
    • Livewire Resources
    • Naming Livewire Components
    • Dynamic Cascading Dropdown with Livewire
    • Hiding a button after click
    • Working with Javascript Components
    • SweetAlert2 with Livewire
    • Select Multiple or Checkboxes
    • Clearing checkboxes in Livewire
    • Livewire File Uploads Using S3
    • Simple Log File Viewer
  • Related Resources
    • Testing resources
    • When Composer runs out of memory
    • Deployment
    • Security
    • Scheduler & Cron tips
    • LastPass tips
    • Using Git
    • VSCode Tips
    • Markdown
    • Cpanel resources
Powered by GitBook
On this page

Was this helpful?

  1. Laravel

Using Spatie Valuestore to hold frequently accessed settings

Domain specific settings

PreviousUsing a mutator to save currencyNextUsing the old() helper

Last updated 4 years ago

Was this helpful?

This was a reply posted to the . I know I will use this in the future, so documented here also as a reminder to myself as much as anything.

Install Valuestore composer require spatie/valuestore

In AppServiceProvider register method;

 public function register()
 {
   $this->app->singleton('valuestore', function () {
     return \Spatie\Valuestore\Valuestore::make(storage_path('app/settings.json'));
   });

   $values = $this->app->valuestore->all();

   $this->app->bind('settings', function () use($values) {
     return $values;
   });
 }

What we have in the app container is a singleton that points to the valuestore class. When you use that, you are directly interacting with the settings stored in the file.

When you use the settings bound to the app container, you are using a cached version of the values as they were at the start of the request cycle (as an associated array).

So instead of writing the current Euro rate to a database row, put it in the valuestore instead;

app('valuestore')->put('EUR', $rate)

and in your model when you want to apply this;

public function getPriceEUR()
{
  return intval($this->usd_price / app('settings')['EUR'] * 100);}
}

By using settings and not valuestore the file will only be accessed once and not for each iterated product in a collection.

Of course you now have a place where you can store other currencies or any other application settings using the full features of the package.

Be careful with decimals when converting . See

Laracasts forum
Valuestore
https://laracasts.com/discuss/channels/general-discussion/int-conversion-issue