[REFACTOR] 優化資料庫查詢效能:在多個 Service 與 Controller 中加入 select 欄位限制,並新增租戶資料表索引 Migration。
This commit is contained in:
@@ -151,7 +151,7 @@ class RoleController extends Controller
|
||||
*/
|
||||
private function getGroupedPermissions()
|
||||
{
|
||||
$allPermissions = Permission::orderBy('name')->get();
|
||||
$allPermissions = Permission::select('id', 'name', 'guard_name')->orderBy('name')->get();
|
||||
$grouped = [];
|
||||
|
||||
foreach ($allPermissions as $permission) {
|
||||
|
||||
@@ -16,7 +16,7 @@ class CoreService implements CoreServiceInterface
|
||||
*/
|
||||
public function getUsersByIds(array $ids): Collection
|
||||
{
|
||||
return User::whereIn('id', $ids)->get();
|
||||
return User::select('id', 'name')->whereIn('id', $ids)->get();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,7 +37,7 @@ class CoreService implements CoreServiceInterface
|
||||
*/
|
||||
public function getAllUsers(): Collection
|
||||
{
|
||||
return User::all();
|
||||
return User::select('id', 'name')->get();
|
||||
}
|
||||
|
||||
public function ensureSystemUserExists()
|
||||
|
||||
@@ -63,7 +63,7 @@ class AdjustDocController extends Controller
|
||||
|
||||
return Inertia::render('Inventory/Adjust/Index', [
|
||||
'docs' => $docs,
|
||||
'warehouses' => Warehouse::all()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
|
||||
'warehouses' => Warehouse::select('id', 'name')->get()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
|
||||
'filters' => $request->only(['warehouse_id', 'search', 'per_page']),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ class CountDocController extends Controller
|
||||
|
||||
return Inertia::render('Inventory/Count/Index', [
|
||||
'docs' => $docs,
|
||||
'warehouses' => Warehouse::all()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
|
||||
'warehouses' => Warehouse::select('id', 'name')->get()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
|
||||
'filters' => $request->only(['warehouse_id', 'search', 'per_page']),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ class InventoryController extends Controller
|
||||
'inventories.lastIncomingTransaction',
|
||||
'inventories.lastOutgoingTransaction'
|
||||
]);
|
||||
$allProducts = Product::with('category')->get();
|
||||
$allProducts = Product::select('id', 'name', 'code', 'category_id')->with('category:id,name')->get();
|
||||
|
||||
// 1. 準備 availableProducts
|
||||
$availableProducts = $allProducts->map(function ($product) {
|
||||
@@ -167,8 +167,8 @@ class InventoryController extends Controller
|
||||
public function create(Warehouse $warehouse)
|
||||
{
|
||||
// ... (unchanged) ...
|
||||
$products = Product::with(['baseUnit', 'largeUnit'])
|
||||
->select('id', 'name', 'code', 'barcode', 'base_unit_id', 'large_unit_id', 'conversion_rate', 'cost_price')
|
||||
$products = Product::select('id', 'name', 'code', 'barcode', 'base_unit_id', 'large_unit_id', 'conversion_rate', 'cost_price')
|
||||
->with(['baseUnit:id,name', 'largeUnit:id,name'])
|
||||
->get()
|
||||
->map(function ($product) {
|
||||
return [
|
||||
|
||||
@@ -112,12 +112,12 @@ class ProductController extends Controller
|
||||
];
|
||||
});
|
||||
|
||||
$categories = Category::where('is_active', true)->get();
|
||||
$categories = Category::select('id', 'name')->where('is_active', true)->get();
|
||||
|
||||
return Inertia::render('Product/Index', [
|
||||
'products' => $products,
|
||||
'categories' => Category::where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
|
||||
'units' => Unit::all()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
|
||||
'categories' => $categories->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
|
||||
'units' => Unit::select('id', 'name', 'code')->get()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
|
||||
'filters' => $request->only(['search', 'category_id', 'per_page', 'sort_field', 'sort_direction']),
|
||||
]);
|
||||
}
|
||||
@@ -172,8 +172,8 @@ class ProductController extends Controller
|
||||
public function create(): Response
|
||||
{
|
||||
return Inertia::render('Product/Create', [
|
||||
'categories' => Category::where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
|
||||
'units' => Unit::all()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
|
||||
'categories' => Category::select('id', 'name')->where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
|
||||
'units' => Unit::select('id', 'name', 'code')->get()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -231,8 +231,8 @@ class ProductController extends Controller
|
||||
'wholesale_price' => (float) $product->wholesale_price,
|
||||
'is_active' => (bool) $product->is_active,
|
||||
],
|
||||
'categories' => Category::where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
|
||||
'units' => Unit::all()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
|
||||
'categories' => Category::select('id', 'name')->where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
|
||||
'units' => Unit::select('id', 'name', 'code')->get()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ class SafetyStockController extends Controller
|
||||
*/
|
||||
public function index(Warehouse $warehouse)
|
||||
{
|
||||
$allProducts = Product::with(['category', 'baseUnit'])->get();
|
||||
$allProducts = Product::select('id', 'name', 'category_id', 'base_unit_id')->with(['category:id,name', 'baseUnit:id,name'])->get();
|
||||
|
||||
// 準備可選商品列表
|
||||
$availableProducts = $allProducts->map(function ($product) {
|
||||
|
||||
@@ -65,7 +65,7 @@ class TransferOrderController extends Controller
|
||||
|
||||
return Inertia::render('Inventory/Transfer/Index', [
|
||||
'orders' => $orders,
|
||||
'warehouses' => Warehouse::all()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
|
||||
'warehouses' => Warehouse::select('id', 'name')->get()->map(fn($w) => ['id' => (string)$w->id, 'name' => $w->name]),
|
||||
'filters' => $request->only(['search', 'warehouse_id', 'per_page']),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ class InventoryService implements InventoryServiceInterface
|
||||
{
|
||||
public function getAllWarehouses()
|
||||
{
|
||||
return Warehouse::all();
|
||||
return Warehouse::select('id', 'name', 'code', 'type')->get();
|
||||
}
|
||||
|
||||
public function getTopInventoryValue(int $limit = 5): \Illuminate\Support\Collection
|
||||
@@ -38,12 +38,14 @@ class InventoryService implements InventoryServiceInterface
|
||||
|
||||
public function getAllProducts()
|
||||
{
|
||||
return Product::with(['baseUnit', 'largeUnit'])->get();
|
||||
return Product::select('id', 'name', 'code', 'base_unit_id', 'large_unit_id')
|
||||
->with(['baseUnit:id,name', 'largeUnit:id,name'])
|
||||
->get();
|
||||
}
|
||||
|
||||
public function getUnits()
|
||||
{
|
||||
return \App\Modules\Inventory\Models\Unit::all();
|
||||
return \App\Modules\Inventory\Models\Unit::select('id', 'name')->get();
|
||||
}
|
||||
|
||||
public function getInventoriesByIds(array $ids, array $with = [])
|
||||
|
||||
@@ -118,7 +118,7 @@ class PurchaseOrderController extends Controller
|
||||
public function create()
|
||||
{
|
||||
// 1. 獲取廠商(無關聯)
|
||||
$vendors = Vendor::all();
|
||||
$vendors = Vendor::select('id', 'name')->get();
|
||||
|
||||
// 2. 手動注入:獲取 Pivot 資料
|
||||
$vendorIds = $vendors->pluck('id')->toArray();
|
||||
@@ -379,7 +379,7 @@ class PurchaseOrderController extends Controller
|
||||
$order = PurchaseOrder::with(['items'])->findOrFail($id);
|
||||
|
||||
// 2. 獲取廠商與商品(與 create 邏輯一致)
|
||||
$vendors = Vendor::all();
|
||||
$vendors = Vendor::select('id', 'name')->get();
|
||||
$vendorIds = $vendors->pluck('id')->toArray();
|
||||
$pivots = DB::table('product_vendor')->whereIn('vendor_id', $vendorIds)->get();
|
||||
$productIds = $pivots->pluck('product_id')->unique()->toArray();
|
||||
|
||||
@@ -48,7 +48,7 @@ class PurchaseReturnController extends Controller
|
||||
{
|
||||
// 取得可用的倉庫與廠商資料供前端選單使用
|
||||
$warehouses = $this->inventoryService->getAllWarehouses();
|
||||
$vendors = Vendor::all();
|
||||
$vendors = Vendor::select('id', 'name')->get();
|
||||
|
||||
// 手動注入:獲取廠商商品 (與 PurchaseOrderController 邏輯一致)
|
||||
$vendorIds = $vendors->pluck('id')->toArray();
|
||||
@@ -157,7 +157,7 @@ class PurchaseReturnController extends Controller
|
||||
});
|
||||
|
||||
$warehouses = $this->inventoryService->getAllWarehouses();
|
||||
$vendors = Vendor::all();
|
||||
$vendors = Vendor::select('id', 'name')->get();
|
||||
|
||||
// 手動注入:獲取廠商商品 (與 create 邏輯一致)
|
||||
$vendorIds = $vendors->pluck('id')->toArray();
|
||||
|
||||
Reference in New Issue
Block a user