Skip to content
Discussion options

You must be logged in to vote

This can be done quite simply. Let's say we have a user and we want to display data in the language and time zone selected for him.

Then we can create in the user model, something like the following method, which would set values for PHP and Laravel:

  /**
   * @return $this
   */
  public function useSettingsAsGlobal(): self
  {
      date_default_timezone_set($this->timezone ?? config('app.timezone'));
      config()->set('app.timezone', $this->timezone ?? config('app.timezone'));

      $locale = $this->locale ?? config('app.locale');

      app()->setLocale($locale);

      \Carbon\Carbon::setLocale($locale);
      \Carbon\CarbonInterval::setLocale($locale);

      return $this;
  }

T…

Replies: 3 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@fonpacific
Comment options

Answer selected by fonpacific
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants