fix: eager loading for db transactions across multiple components to prevent N+1 query issues.
This commit is contained in:
parent
b5d8499ac7
commit
89b600dec9
|
|
@ -34,9 +34,33 @@ class Documents extends ApiController
|
|||
{
|
||||
// Check if we're querying by id or number
|
||||
if (is_numeric($id)) {
|
||||
$document = Document::find($id);
|
||||
$document = Document::with([
|
||||
'contact',
|
||||
'histories',
|
||||
'items',
|
||||
'items.taxes',
|
||||
'items.taxes.tax',
|
||||
'item_taxes',
|
||||
'totals',
|
||||
'transactions',
|
||||
'transactions.currency',
|
||||
'transactions.account',
|
||||
'transactions.category',
|
||||
])->find($id);
|
||||
} else {
|
||||
$document = Document::where('document_number', $id)->first();
|
||||
$document = Document::with([
|
||||
'contact',
|
||||
'histories',
|
||||
'items',
|
||||
'items.taxes',
|
||||
'items.taxes.tax',
|
||||
'item_taxes',
|
||||
'totals',
|
||||
'transactions',
|
||||
'transactions.currency',
|
||||
'transactions.account',
|
||||
'transactions.category',
|
||||
])->where('document_number', $id)->first();
|
||||
}
|
||||
|
||||
if (! $document instanceof Document) {
|
||||
|
|
|
|||
|
|
@ -382,6 +382,11 @@ class Document extends Model
|
|||
$rate = $this->currency_rate;
|
||||
$precision = currency($code)->getPrecision();
|
||||
|
||||
// Lazy eager load transactions if not already loaded to prevent N+1 queries
|
||||
if (!$this->relationLoaded('transactions')) {
|
||||
$this->load('transactions');
|
||||
}
|
||||
|
||||
if ($this->transactions->count()) {
|
||||
foreach ($this->transactions as $transaction) {
|
||||
$amount = $transaction->amount;
|
||||
|
|
@ -414,6 +419,11 @@ class Document extends Model
|
|||
$rate = $this->currency_rate;
|
||||
$precision = currency($code)->getPrecision();
|
||||
|
||||
// Lazy eager load transactions if not already loaded to prevent N+1 queries
|
||||
if (!$this->relationLoaded('transactions')) {
|
||||
$this->load('transactions');
|
||||
}
|
||||
|
||||
if ($this->transactions->count()) {
|
||||
foreach ($this->transactions as $transaction) {
|
||||
$amount = $transaction->amount;
|
||||
|
|
@ -423,7 +433,7 @@ class Document extends Model
|
|||
}
|
||||
|
||||
if ($transaction->reconciled) {
|
||||
$reconciled_amount = +$amount;
|
||||
$reconciled_amount += $amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -206,7 +206,8 @@ trait Documents
|
|||
|
||||
$today = Date::today()->toDateString();
|
||||
|
||||
$documents = $documents ?: Document::type($type)->with('transactions')->future();
|
||||
// Eager load transactions with currency to prevent N+1 queries when calling getAmountConvertedToDefault()
|
||||
$documents = $documents ?: Document::type($type)->with(['transactions.currency'])->future();
|
||||
|
||||
$documents->each(function ($document) use (&$totals, $today) {
|
||||
if (! in_array($document->status, $this->getDocumentStatusesForFuture())) {
|
||||
|
|
|
|||
|
|
@ -36,7 +36,8 @@ class Content extends Component
|
|||
// Handle documents
|
||||
$docs = $this->contact->isCustomer() ? 'invoices' : 'bills';
|
||||
|
||||
$this->documents = $this->contact->$docs()->with('transactions')->get();
|
||||
// Eager load transactions with currency to prevent N+1 queries when calling getAmountConvertedToDefault()
|
||||
$this->documents = $this->contact->$docs()->with(['transactions.currency'])->get();
|
||||
|
||||
$this->counts['documents'] = $this->documents->count();
|
||||
|
||||
|
|
@ -62,8 +63,8 @@ class Content extends Component
|
|||
}
|
||||
}
|
||||
|
||||
// Handle payments
|
||||
$this->transactions = $this->contact->transactions()->with('account', 'category')->get();
|
||||
// Handle payments - eager load currency to prevent N+1 queries
|
||||
$this->transactions = $this->contact->transactions()->with(['account', 'category', 'currency'])->get();
|
||||
|
||||
$this->counts['transactions'] = $this->transactions->count();
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,11 @@ class Attachment extends Component
|
|||
{
|
||||
$this->transaction_attachment = collect();
|
||||
|
||||
// Eager load transactions with their media/attachments to prevent N+1 queries
|
||||
if (!$this->document->relationLoaded('transactions')) {
|
||||
$this->document->load(['transactions.media', 'transactions']);
|
||||
}
|
||||
|
||||
if ($this->document->transactions->count()) {
|
||||
foreach ($this->document->transactions as $transaction) {
|
||||
if (! $transaction->attachment) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue