Files
star-cloud/app/Traits/TenantScoped.php
sky121113 3ce88ed342 [FEAT] 重構機台日誌 UI 與增加多語系支援,並整合 IoT API 核心架構
- 機台日誌:對齊 Luxury UI 規範,實作整合式佈局與分頁組件。
- 多語系:完成機台日誌繁、英、日三語系翻譯與動態處理。
- UI 規範:更新 SKILL.md 定義「標準列表 Bible」。
- 後端:完善 TenantScoped 隔離邏輯,修復儀表板死循環與 User Model 缺失。
- IoT:擴展機台、會員 Model 並建立交易、商品、狀態等核心表結構。
- 基礎設施:設置台北時區與 Docker 環境變數同步。
2026-03-16 17:29:15 +08:00

52 lines
1.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Traits;
use Illuminate\Database\Eloquent\Builder;
trait TenantScoped
{
/**
* Boot the trait.
*/
public static function bootTenantScoped(): void
{
static::addGlobalScope('tenant', function (Builder $query) {
// 避免在 User Model 本身套用此 Scope否則在 auth()->user() 讀取 User 時會產生循環引用
if (static::class === \App\Models\System\User::class) {
return;
}
// check if running in console/migration
if (app()->runningInConsole()) {
return;
}
$user = auth()->user();
// 如果使用者已登入且有綁定公司,則自動注入過濾條件
if ($user && $user->company_id) {
$query->where((new static)->getTable() . '.company_id', $user->company_id);
}
});
// 建立資料時,自動填入當前使用者的 company_id
static::creating(function ($model) {
if (!$model->company_id) {
$user = auth()->user();
if ($user && $user->company_id) {
$model->company_id = $user->company_id;
}
}
});
}
/**
* Define the company relationship.
*/
public function company()
{
return $this->belongsTo(\App\Models\System\Company::class);
}
}