F
Filament3mo ago
dyo

Optimize duplicate queries in 2 method at the same TextColumn Class

Can someone help me to optimize with this case, TextColumn::make('jenisDonasi.nama_jenis') ->tooltip(function ($record) { if ($record->campaign) { return 'Campaign: ' . $record->campaign->judul; } }) ->description(function ($record) { if ($record->campaign) { return 'Lihat campaign'; } }) The $record->campaign is a belongsTo relationship query, which is duplicate. How can I execute only one $record->campaign to both method above in TextColumn?
7 Replies
dyo
dyoOP3mo ago
can someone help me?
rajul
rajul3mo ago
you could create a function and cache the result of relation in that function and on subsequent call it will return cache result, something like this: https://laravel.com/docs/11.x/helpers#method-once
protected function getCampaignInfo(Model $record)
return once(function () use ($record) {
return $record->campaign;
});
}
protected function getCampaignInfo(Model $record)
return once(function () use ($record) {
return $record->campaign;
});
}
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Matthew
Matthew3mo ago
For performance, or readability ?
dyo
dyoOP3mo ago
thanks a lot for the suggestion.. unfortunately, I currently using laravel 10.x for performance, any other idea?
Matthew
Matthew3mo ago
Have you checked whether that is creating duplicate database calls ?
dyo
dyoOP3mo ago
my bad, after I double check, it's not duplicate..
Matthew
Matthew3mo ago
Good news, otherwise that would have undone a whole load of my understanding of how laravel operates with the DB!!
Want results from more Discord servers?
Add your server