[FIX] 修復商品多語系儲存與讀取錯誤、新增自動語系名稱顯示、補強商品規格欄位及密碼顯示切換功能
All checks were successful
star-cloud-deploy-demo / deploy-demo (push) Successful in 47s

This commit is contained in:
2026-03-30 17:11:15 +08:00
parent 9bbfaa39e6
commit d14eda7d69
9 changed files with 167 additions and 40 deletions

View File

@@ -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