F
Filament4w 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
dyo4w ago
can someone help me?
rajul
rajul4w 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
Matthew4w ago
For performance, or readability ?
dyo
dyo4w ago
thanks a lot for the suggestion.. unfortunately, I currently using laravel 10.x for performance, any other idea?
Matthew
Matthew4w ago
Have you checked whether that is creating duplicate database calls ?
dyo
dyo4w ago
my bad, after I double check, it's not duplicate..
Matthew
Matthew4w 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