withoutGlobalScopes() ->whereNotNull('transactionable_id') ->whereNull('payeeable_id') ->with('transactionable') ->get(); foreach ($transactions as $transaction) { $document = $transaction->transactionable; if ($document instanceof Invoice) { $transaction->payeeable_id = $document->client_id; $transaction->payeeable_type = Client::class; $transaction->saveQuietly(); } elseif ($document instanceof Bill) { $transaction->payeeable_id = $document->vendor_id; $transaction->payeeable_type = Vendor::class; $transaction->saveQuietly(); } } } };