您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

2025_05_19_220942_update_transaction_payees.php 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. use App\Models\Accounting\Bill;
  3. use App\Models\Accounting\Invoice;
  4. use App\Models\Accounting\Transaction;
  5. use App\Models\Common\Client;
  6. use App\Models\Common\Vendor;
  7. use Illuminate\Database\Migrations\Migration;
  8. return new class extends Migration
  9. {
  10. /**
  11. * Run the migrations.
  12. */
  13. public function up(): void
  14. {
  15. $transactions = Transaction::query()
  16. ->withoutGlobalScopes()
  17. ->whereHasMorph('transactionable', [Invoice::class, Bill::class])
  18. ->whereDoesntHaveMorph('payeeable', [Client::class, Vendor::class])
  19. ->get();
  20. foreach ($transactions as $transaction) {
  21. $document = $transaction->transactionable;
  22. if ($document instanceof Invoice) {
  23. $transaction->payeeable_id = $document->client_id;
  24. $transaction->payeeable_type = Client::class;
  25. $transaction->saveQuietly();
  26. } elseif ($document instanceof Bill) {
  27. $transaction->payeeable_id = $document->vendor_id;
  28. $transaction->payeeable_type = Vendor::class;
  29. $transaction->saveQuietly();
  30. }
  31. }
  32. }
  33. };