From fe5dfd382f56e61c3b33d2a8879b93b30f1f5227 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Fri, 10 Jul 2026 11:07:07 +0700 Subject: [PATCH] refactor: update relationships in models to include withTrashed() for soft deletes, ensuring proper handling of related records --- app/Models/Attendance.php | 2 +- app/Models/CashTransaction.php | 2 +- app/Models/Cutting.php | 4 ++-- app/Models/CuttingMaterial.php | 8 ++++---- app/Models/CuttingMaterialCombination.php | 4 ++-- app/Models/CuttingResult.php | 6 +++--- app/Models/CuttingResultPrice.php | 4 ++-- app/Models/Employee.php | 2 +- app/Models/EmployeeAdvance.php | 2 +- app/Models/EmployeeAdvancePayment.php | 2 +- app/Models/Expense.php | 4 ++-- app/Models/LeaveRequest.php | 2 +- app/Models/Notification.php | 2 +- app/Models/Order.php | 8 ++++---- app/Models/OrderItem.php | 6 +++--- app/Models/OwnerVerificationRequest.php | 4 ++-- app/Models/Payroll.php | 2 +- app/Models/PayrollAdjustment.php | 2 +- app/Models/PayrollPeriod.php | 2 +- app/Models/ProductPrice.php | 2 +- app/Models/ProductVariant.php | 2 +- app/Models/Purchase.php | 2 +- app/Models/PurchaseItem.php | 6 +++--- app/Models/RawMaterialPrice.php | 2 +- app/Models/Rejection.php | 2 +- app/Models/RetailStockHistory.php | 4 ++-- app/Models/StokOpname.php | 4 ++-- app/Models/StokOpnameItem.php | 4 ++-- app/Models/UserProfile.php | 2 +- 29 files changed, 49 insertions(+), 49 deletions(-) diff --git a/app/Models/Attendance.php b/app/Models/Attendance.php index d11e2a0..83d5307 100644 --- a/app/Models/Attendance.php +++ b/app/Models/Attendance.php @@ -130,7 +130,7 @@ public function registerMediaCollections(): void // 5. Relation public function employee(): BelongsTo { - return $this->belongsTo(Employee::class); + return $this->belongsTo(Employee::class)->withTrashed(); } public function payrollAdjustments(): HasMany diff --git a/app/Models/CashTransaction.php b/app/Models/CashTransaction.php index e970afc..5e90fed 100644 --- a/app/Models/CashTransaction.php +++ b/app/Models/CashTransaction.php @@ -191,7 +191,7 @@ public function cashAccount(): BelongsTo public function createdBy(): BelongsTo { - return $this->belongsTo(User::class, 'created_by_id'); + return $this->belongsTo(User::class, 'created_by_id')->withTrashed(); } public function order(): HasOne diff --git a/app/Models/Cutting.php b/app/Models/Cutting.php index 89c628c..2b4cfc9 100644 --- a/app/Models/Cutting.php +++ b/app/Models/Cutting.php @@ -105,7 +105,7 @@ public function ensureEditable(): void // 6. Relation public function createdBy(): BelongsTo { - return $this->belongsTo(User::class, 'created_by_id'); + return $this->belongsTo(User::class, 'created_by_id')->withTrashed(); } public function materials(): HasMany @@ -130,6 +130,6 @@ public function results(): HasMany public function submittedBy(): BelongsTo { - return $this->belongsTo(User::class, 'submitted_by_id'); + return $this->belongsTo(User::class, 'submitted_by_id')->withTrashed(); } } diff --git a/app/Models/CuttingMaterial.php b/app/Models/CuttingMaterial.php index d3f0275..507a312 100644 --- a/app/Models/CuttingMaterial.php +++ b/app/Models/CuttingMaterial.php @@ -81,21 +81,21 @@ private function formatQuantityInput(float|string|null $value): string // 5. Relation public function cutting(): BelongsTo { - return $this->belongsTo(Cutting::class); + return $this->belongsTo(Cutting::class)->withTrashed(); } public function rawMaterialPrice(): BelongsTo { - return $this->belongsTo(RawMaterialPrice::class); + return $this->belongsTo(RawMaterialPrice::class)->withTrashed(); } public function combination(): BelongsTo { - return $this->belongsTo(CuttingMaterialCombination::class, 'combination_id'); + return $this->belongsTo(CuttingMaterialCombination::class, 'combination_id')->withTrashed(); } public function user(): BelongsTo { - return $this->belongsTo(User::class); + return $this->belongsTo(User::class)->withTrashed(); } } diff --git a/app/Models/CuttingMaterialCombination.php b/app/Models/CuttingMaterialCombination.php index 201b7e0..ad3379d 100644 --- a/app/Models/CuttingMaterialCombination.php +++ b/app/Models/CuttingMaterialCombination.php @@ -24,12 +24,12 @@ protected function casts(): array public function cutting(): BelongsTo { - return $this->belongsTo(Cutting::class); + return $this->belongsTo(Cutting::class)->withTrashed(); } public function user(): BelongsTo { - return $this->belongsTo(User::class); + return $this->belongsTo(User::class)->withTrashed(); } public function materials(): HasMany diff --git a/app/Models/CuttingResult.php b/app/Models/CuttingResult.php index 9ff2f78..9d8778e 100644 --- a/app/Models/CuttingResult.php +++ b/app/Models/CuttingResult.php @@ -28,16 +28,16 @@ protected function casts(): array // 3. Relation public function cutting(): BelongsTo { - return $this->belongsTo(Cutting::class); + return $this->belongsTo(Cutting::class)->withTrashed(); } public function productVariant(): BelongsTo { - return $this->belongsTo(ProductVariant::class); + return $this->belongsTo(ProductVariant::class)->withTrashed(); } public function user(): BelongsTo { - return $this->belongsTo(User::class); + return $this->belongsTo(User::class)->withTrashed(); } } diff --git a/app/Models/CuttingResultPrice.php b/app/Models/CuttingResultPrice.php index 932332c..bb51a1f 100644 --- a/app/Models/CuttingResultPrice.php +++ b/app/Models/CuttingResultPrice.php @@ -53,11 +53,11 @@ public function typeLabel(): Attribute // 4. Relation public function cutting(): BelongsTo { - return $this->belongsTo(Cutting::class); + return $this->belongsTo(Cutting::class)->withTrashed(); } public function productVariant(): BelongsTo { - return $this->belongsTo(ProductVariant::class); + return $this->belongsTo(ProductVariant::class)->withTrashed(); } } diff --git a/app/Models/Employee.php b/app/Models/Employee.php index 1a730f2..04e1b36 100644 --- a/app/Models/Employee.php +++ b/app/Models/Employee.php @@ -118,6 +118,6 @@ public function payrolls(): HasMany public function user(): BelongsTo { - return $this->belongsTo(User::class); + return $this->belongsTo(User::class)->withTrashed(); } } diff --git a/app/Models/EmployeeAdvance.php b/app/Models/EmployeeAdvance.php index 6bb5841..66b4c83 100644 --- a/app/Models/EmployeeAdvance.php +++ b/app/Models/EmployeeAdvance.php @@ -201,7 +201,7 @@ public function cashTransaction(): BelongsTo public function employee(): BelongsTo { - return $this->belongsTo(Employee::class); + return $this->belongsTo(Employee::class)->withTrashed(); } public function paidBy(): BelongsTo diff --git a/app/Models/EmployeeAdvancePayment.php b/app/Models/EmployeeAdvancePayment.php index c74e67c..210912d 100644 --- a/app/Models/EmployeeAdvancePayment.php +++ b/app/Models/EmployeeAdvancePayment.php @@ -56,6 +56,6 @@ public function employeeAdvance(): BelongsTo public function paidBy(): BelongsTo { - return $this->belongsTo(User::class, 'paid_by_id'); + return $this->belongsTo(User::class, 'paid_by_id')->withTrashed(); } } diff --git a/app/Models/Expense.php b/app/Models/Expense.php index bbb12e4..428e1bd 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -64,11 +64,11 @@ public function registerMediaCollections(): void // 5. Relation public function cashTransaction(): BelongsTo { - return $this->belongsTo(CashTransaction::class); + return $this->belongsTo(CashTransaction::class)->withTrashed(); } public function createdBy(): BelongsTo { - return $this->belongsTo(User::class, 'created_by_id'); + return $this->belongsTo(User::class, 'created_by_id')->withTrashed(); } } diff --git a/app/Models/LeaveRequest.php b/app/Models/LeaveRequest.php index 387b466..a82f758 100644 --- a/app/Models/LeaveRequest.php +++ b/app/Models/LeaveRequest.php @@ -158,7 +158,7 @@ public static function canSubmit(User $user): bool // 6. Relation public function employee(): BelongsTo { - return $this->belongsTo(Employee::class); + return $this->belongsTo(Employee::class)->withTrashed(); } public function verifiedBy(): BelongsTo diff --git a/app/Models/Notification.php b/app/Models/Notification.php index dc816e2..76b9e81 100644 --- a/app/Models/Notification.php +++ b/app/Models/Notification.php @@ -45,6 +45,6 @@ public function markAsRead(): void // 4. Relation public function user(): BelongsTo { - return $this->belongsTo(User::class); + return $this->belongsTo(User::class)->withTrashed(); } } diff --git a/app/Models/Order.php b/app/Models/Order.php index dbde917..94b310e 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -289,17 +289,17 @@ public function registerMediaCollections(): void // 6. Relation public function cashTransaction(): BelongsTo { - return $this->belongsTo(CashTransaction::class); + return $this->belongsTo(CashTransaction::class)->withTrashed(); } public function createdBy(): BelongsTo { - return $this->belongsTo(User::class, 'created_by_id'); + return $this->belongsTo(User::class, 'created_by_id')->withTrashed(); } public function customer(): BelongsTo { - return $this->belongsTo(Customer::class); + return $this->belongsTo(Customer::class)->withTrashed(); } public function items(): HasMany @@ -309,6 +309,6 @@ public function items(): HasMany public function marketing(): BelongsTo { - return $this->belongsTo(User::class, 'marketing_id'); + return $this->belongsTo(User::class, 'marketing_id')->withTrashed(); } } diff --git a/app/Models/OrderItem.php b/app/Models/OrderItem.php index d1e7b25..e1a424b 100644 --- a/app/Models/OrderItem.php +++ b/app/Models/OrderItem.php @@ -67,16 +67,16 @@ public function unitPriceFormatted(): Attribute // 4. Relation public function order(): BelongsTo { - return $this->belongsTo(Order::class); + return $this->belongsTo(Order::class)->withTrashed(); } public function productVariant(): BelongsTo { - return $this->belongsTo(ProductVariant::class); + return $this->belongsTo(ProductVariant::class)->withTrashed(); } public function user(): BelongsTo { - return $this->belongsTo(User::class); + return $this->belongsTo(User::class)->withTrashed(); } } diff --git a/app/Models/OwnerVerificationRequest.php b/app/Models/OwnerVerificationRequest.php index 2b0c7cd..2adf03a 100644 --- a/app/Models/OwnerVerificationRequest.php +++ b/app/Models/OwnerVerificationRequest.php @@ -184,11 +184,11 @@ public function subject(): MorphTo public function submittedBy(): BelongsTo { - return $this->belongsTo(User::class, 'submitted_by_id'); + return $this->belongsTo(User::class, 'submitted_by_id')->withTrashed(); } public function verifiedBy(): BelongsTo { - return $this->belongsTo(User::class, 'verified_by_id'); + return $this->belongsTo(User::class, 'verified_by_id')->withTrashed(); } } diff --git a/app/Models/Payroll.php b/app/Models/Payroll.php index 4129c60..be0f568 100644 --- a/app/Models/Payroll.php +++ b/app/Models/Payroll.php @@ -163,7 +163,7 @@ public function cashTransaction(): BelongsTo public function employee(): BelongsTo { - return $this->belongsTo(Employee::class); + return $this->belongsTo(Employee::class)->withTrashed(); } public function paidBy(): BelongsTo diff --git a/app/Models/PayrollAdjustment.php b/app/Models/PayrollAdjustment.php index d0a16d8..54fd858 100644 --- a/app/Models/PayrollAdjustment.php +++ b/app/Models/PayrollAdjustment.php @@ -83,7 +83,7 @@ public function attendance(): BelongsTo public function createdBy(): BelongsTo { - return $this->belongsTo(User::class, 'created_by_id'); + return $this->belongsTo(User::class, 'created_by_id')->withTrashed(); } public function payroll(): BelongsTo diff --git a/app/Models/PayrollPeriod.php b/app/Models/PayrollPeriod.php index 7a52998..6c8f178 100644 --- a/app/Models/PayrollPeriod.php +++ b/app/Models/PayrollPeriod.php @@ -77,7 +77,7 @@ public function isOpen(): bool // 6. Relation public function closedBy(): BelongsTo { - return $this->belongsTo(User::class, 'closed_by_id'); + return $this->belongsTo(User::class, 'closed_by_id')->withTrashed(); } public function payrolls(): HasMany diff --git a/app/Models/ProductPrice.php b/app/Models/ProductPrice.php index a4c01f0..5be5527 100644 --- a/app/Models/ProductPrice.php +++ b/app/Models/ProductPrice.php @@ -44,6 +44,6 @@ public function priceInput(): Attribute // 4. Relation public function variant(): BelongsTo { - return $this->belongsTo(ProductVariant::class, 'variant_id'); + return $this->belongsTo(ProductVariant::class, 'variant_id')->withTrashed(); } } diff --git a/app/Models/ProductVariant.php b/app/Models/ProductVariant.php index 0d50c2f..b539200 100644 --- a/app/Models/ProductVariant.php +++ b/app/Models/ProductVariant.php @@ -94,6 +94,6 @@ public function prices(): HasMany public function product(): BelongsTo { - return $this->belongsTo(Product::class); + return $this->belongsTo(Product::class)->withTrashed(); } } diff --git a/app/Models/Purchase.php b/app/Models/Purchase.php index 067d694..8bd4e91 100644 --- a/app/Models/Purchase.php +++ b/app/Models/Purchase.php @@ -114,6 +114,6 @@ public function pendingOwnerVerificationRequest(): MorphOne public function supplier(): BelongsTo { - return $this->belongsTo(Supplier::class); + return $this->belongsTo(Supplier::class)->withTrashed(); } } diff --git a/app/Models/PurchaseItem.php b/app/Models/PurchaseItem.php index 16d3751..9eab57c 100644 --- a/app/Models/PurchaseItem.php +++ b/app/Models/PurchaseItem.php @@ -80,16 +80,16 @@ public function unitPriceFormatted(): Attribute // 4. Relation public function purchase(): BelongsTo { - return $this->belongsTo(Purchase::class); + return $this->belongsTo(Purchase::class)->withTrashed(); } public function rawMaterialPrice(): BelongsTo { - return $this->belongsTo(RawMaterialPrice::class); + return $this->belongsTo(RawMaterialPrice::class)->withTrashed(); } public function user(): BelongsTo { - return $this->belongsTo(User::class); + return $this->belongsTo(User::class)->withTrashed(); } } diff --git a/app/Models/RawMaterialPrice.php b/app/Models/RawMaterialPrice.php index 4379899..a63cb89 100644 --- a/app/Models/RawMaterialPrice.php +++ b/app/Models/RawMaterialPrice.php @@ -89,6 +89,6 @@ public function purchaseItems(): HasMany public function rawMaterial(): BelongsTo { - return $this->belongsTo(RawMaterial::class); + return $this->belongsTo(RawMaterial::class)->withTrashed(); } } diff --git a/app/Models/Rejection.php b/app/Models/Rejection.php index ff1cd18..b55a1f0 100644 --- a/app/Models/Rejection.php +++ b/app/Models/Rejection.php @@ -23,6 +23,6 @@ public function rejectable(): MorphTo public function rejectedBy(): BelongsTo { - return $this->belongsTo(User::class, 'rejected_by_id'); + return $this->belongsTo(User::class, 'rejected_by_id')->withTrashed(); } } diff --git a/app/Models/RetailStockHistory.php b/app/Models/RetailStockHistory.php index 0e89ca7..bf84cab 100644 --- a/app/Models/RetailStockHistory.php +++ b/app/Models/RetailStockHistory.php @@ -29,11 +29,11 @@ protected function casts(): array // 2. Relation public function productVariant(): BelongsTo { - return $this->belongsTo(ProductVariant::class); + return $this->belongsTo(ProductVariant::class)->withTrashed(); } public function user(): BelongsTo { - return $this->belongsTo(User::class); + return $this->belongsTo(User::class)->withTrashed(); } } diff --git a/app/Models/StokOpname.php b/app/Models/StokOpname.php index f47b9ec..f0a779a 100644 --- a/app/Models/StokOpname.php +++ b/app/Models/StokOpname.php @@ -89,7 +89,7 @@ public function statusLabel(): Attribute // 5. Relation public function createdBy(): BelongsTo { - return $this->belongsTo(User::class, 'created_by_id'); + return $this->belongsTo(User::class, 'created_by_id')->withTrashed(); } public function items(): HasMany @@ -99,6 +99,6 @@ public function items(): HasMany public function verifiedBy(): BelongsTo { - return $this->belongsTo(User::class, 'verified_by_id'); + return $this->belongsTo(User::class, 'verified_by_id')->withTrashed(); } } diff --git a/app/Models/StokOpnameItem.php b/app/Models/StokOpnameItem.php index bbec05c..59cd787 100644 --- a/app/Models/StokOpnameItem.php +++ b/app/Models/StokOpnameItem.php @@ -43,11 +43,11 @@ public function getVariantNameAttribute(): string // 3. Relation public function productVariant(): BelongsTo { - return $this->belongsTo(ProductVariant::class); + return $this->belongsTo(ProductVariant::class)->withTrashed(); } public function stokOpname(): BelongsTo { - return $this->belongsTo(StokOpname::class); + return $this->belongsTo(StokOpname::class)->withTrashed(); } } diff --git a/app/Models/UserProfile.php b/app/Models/UserProfile.php index 0cb0eb3..45aa2ee 100644 --- a/app/Models/UserProfile.php +++ b/app/Models/UserProfile.php @@ -87,6 +87,6 @@ public function registerMediaCollections(): void // 5. Relation public function user(): BelongsTo { - return $this->belongsTo(User::class); + return $this->belongsTo(User::class)->withTrashed(); } }