From d14eda7d690740afac27e001acf92f6af64e36d1 Mon Sep 17 00:00:00 2001 From: sky121113 Date: Mon, 30 Mar 2026 17:11:15 +0800 Subject: [PATCH] =?UTF-8?q?[FIX]=20=E4=BF=AE=E5=BE=A9=E5=95=86=E5=93=81?= =?UTF-8?q?=E5=A4=9A=E8=AA=9E=E7=B3=BB=E5=84=B2=E5=AD=98=E8=88=87=E8=AE=80?= =?UTF-8?q?=E5=8F=96=E9=8C=AF=E8=AA=A4=E3=80=81=E6=96=B0=E5=A2=9E=E8=87=AA?= =?UTF-8?q?=E5=8B=95=E8=AA=9E=E7=B3=BB=E5=90=8D=E7=A8=B1=E9=A1=AF=E7=A4=BA?= =?UTF-8?q?=E3=80=81=E8=A3=9C=E5=BC=B7=E5=95=86=E5=93=81=E8=A6=8F=E6=A0=BC?= =?UTF-8?q?=E6=AC=84=E4=BD=8D=E5=8F=8A=E5=AF=86=E7=A2=BC=E9=A1=AF=E7=A4=BA?= =?UTF-8?q?=E5=88=87=E6=8F=9B=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Admin/ProductController.php | 24 ++++++---- app/Models/Product/Product.php | 27 +++++++++++ .../views/admin/companies/index.blade.php | 16 ++++++- .../admin/data-config/accounts.blade.php | 18 +++++-- resources/views/admin/products/edit.blade.php | 2 +- .../views/admin/products/index.blade.php | 10 ++-- resources/views/auth/login.blade.php | 23 +++++---- resources/views/auth/reset-password.blade.php | 39 +++++++++++---- .../partials/update-password-form.blade.php | 48 ++++++++++++++++--- 9 files changed, 167 insertions(+), 40 deletions(-) diff --git a/app/Http/Controllers/Admin/ProductController.php b/app/Http/Controllers/Admin/ProductController.php index d7fe895..0d950f0 100644 --- a/app/Http/Controllers/Admin/ProductController.php +++ b/app/Http/Controllers/Admin/ProductController.php @@ -86,7 +86,14 @@ class ProductController extends Controller public function edit($id) { $user = auth()->user(); - $product = Product::with(['translations', 'company'])->findOrFail($id); + // 繞過 TenantScoped 載入翻譯,確保系統管理員能看到租戶公司的翻譯資料 + $product = Product::with(['company'])->findOrFail($id); + $product->setRelation('translations', + Translation::withoutGlobalScopes() + ->where('group', 'product') + ->where('key', $product->name_dictionary_key) + ->get() + ); $categories = ProductCategory::all(); $companies = $user->isSystemAdmin() ? Company::all() : collect(); @@ -131,10 +138,10 @@ class ProductController extends Controller ? $request->company_id : auth()->user()->company_id; - // Store translations + // 儲存多語系翻譯(繞過 TenantScoped,避免系統管理員操作租戶資料時被過濾) foreach ($request->names as $locale => $name) { if (empty($name)) continue; - Translation::create([ + Translation::withoutGlobalScopes()->create([ 'group' => 'product', 'key' => $dictKey, 'locale' => $locale, @@ -219,10 +226,10 @@ class ProductController extends Controller $dictKey = $product->name_dictionary_key ?: \Illuminate\Support\Str::uuid()->toString(); $company_id = $product->company_id; - // Update or Create translations + // 更新或建立多語系翻譯(繞過 TenantScoped,避免系統管理員操作租戶資料時被過濾) foreach ($request->names as $locale => $name) { if (empty($name)) { - Translation::where([ + Translation::withoutGlobalScopes()->where([ 'group' => 'product', 'key' => $dictKey, 'locale' => $locale @@ -230,7 +237,7 @@ class ProductController extends Controller continue; } - Translation::updateOrCreate( + Translation::withoutGlobalScopes()->updateOrCreate( [ 'group' => 'product', 'key' => $dictKey, @@ -246,6 +253,7 @@ class ProductController extends Controller $data = [ 'category_id' => $request->category_id, 'name' => $request->names['zh_TW'] ?? ($product->name ?? 'Untitled'), + 'name_dictionary_key' => $dictKey, 'barcode' => $request->barcode, 'spec' => $request->spec, 'manufacturer' => $request->manufacturer, @@ -316,9 +324,9 @@ class ProductController extends Controller try { $product = Product::findOrFail($id); - // Delete translations associated with this product + // 刪除與此商品關聯的翻譯資料(繞過 TenantScoped) if ($product->name_dictionary_key) { - Translation::where('key', $product->name_dictionary_key)->delete(); + Translation::withoutGlobalScopes()->where('key', $product->name_dictionary_key)->delete(); } // Delete image diff --git a/app/Models/Product/Product.php b/app/Models/Product/Product.php index a34a2de..ffb365d 100644 --- a/app/Models/Product/Product.php +++ b/app/Models/Product/Product.php @@ -48,6 +48,33 @@ class Product extends Model return $this->belongsTo(ProductCategory::class, 'category_id'); } + /** + * 自動附加到 JSON/陣列輸出的屬性(供 Alpine.js 等前端使用) + */ + protected $appends = ['localized_name']; + + /** + * 取得當前語系的商品名稱。 + * 回退順序:當前語系 → zh_TW → name 欄位 + */ + public function getLocalizedNameAttribute(): string + { + if ($this->relationLoaded('translations') && $this->translations->isNotEmpty()) { + $locale = app()->getLocale(); + // 先找當前語系 + $translation = $this->translations->firstWhere('locale', $locale); + if ($translation) { + return $translation->value; + } + // 回退至 zh_TW + $fallback = $this->translations->firstWhere('locale', 'zh_TW'); + if ($fallback) { + return $fallback->value; + } + } + return $this->name ?? ''; + } + /** * Get the translations for the product name. */ diff --git a/resources/views/admin/companies/index.blade.php b/resources/views/admin/companies/index.blade.php index 99c81c3..f0cb779 100644 --- a/resources/views/admin/companies/index.blade.php +++ b/resources/views/admin/companies/index.blade.php @@ -484,8 +484,20 @@
- +
+ + +
diff --git a/resources/views/admin/data-config/accounts.blade.php b/resources/views/admin/data-config/accounts.blade.php index 1aa1504..722d833 100644 --- a/resources/views/admin/data-config/accounts.blade.php +++ b/resources/views/admin/data-config/accounts.blade.php @@ -366,9 +366,21 @@ $roleSelectConfig = [ * - +
+ + +
diff --git a/resources/views/admin/products/edit.blade.php b/resources/views/admin/products/edit.blade.php index cb25a54..6bae50f 100644 --- a/resources/views/admin/products/edit.blade.php +++ b/resources/views/admin/products/edit.blade.php @@ -4,7 +4,7 @@ @php $names = []; foreach(['zh_TW', 'en', 'ja'] as $locale) { - $names[$locale] = $product->translations->where('locale', $locale)->first()?->text ?? ''; + $names[$locale] = $product->translations->where('locale', $locale)->first()?->value ?? ''; } // If zh_TW translation is empty, fallback to product->name if (empty($names['zh_TW'])) { diff --git a/resources/views/admin/products/index.blade.php b/resources/views/admin/products/index.blade.php index c55d92e..3fe27fc 100644 --- a/resources/views/admin/products/index.blade.php +++ b/resources/views/admin/products/index.blade.php @@ -90,7 +90,7 @@ $roleSelectConfig = [ @endif
- {{ $product->name }} + {{ $product->localized_name }}
@php $catName = $product->category->name ?? __('Uncategorized'); @@ -236,7 +236,7 @@ $roleSelectConfig = [

{{ __('Product Details') }}

-

+

+
+ {{ __('Specification') }} +
+