我的品牌表中有列时区。我想让时区动态化,例如应该应用哪个品牌的时区。
public function handle($request, Closure $next)
{
$user = Auth::user();
$brandId = Auth::check() ? Auth::user()->brand : null;
// Fetch the brand
$brand = Brand::find($brandId); // You need to replace $brandId with the actual brand ID
// Set the timezone
if ($brand) {
Config::set('app.timezone', $brand->timezone);
}
}
我将其添加到我的 AdminAccess.php 中间件中,但它没有存储确切的时区
Laravel News 最近发布了与您的问题相关的教程。
这是它的链接
https://laravel-news.com/laravel-timezones
您可以在
Carbon::macro
中创建
AppServiceProvider
Carbon::macro('inBrandTimezone', function () {
$timezone = auth()->user()?->brand?->timezone ?? config('app.timezone');
return $this->tz($timezone);
});
然后您可以在您的应用程序中使用它,例如:
now()->inBrandTimezone()->format('Y-m-d')