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
can someone help me?
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
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.
For performance, or readability ?
thanks a lot for the suggestion..
unfortunately, I currently using laravel 10.x
for performance, any other idea?
Have you checked whether that is creating duplicate database calls ?
my bad, after I double check, it's not duplicate..
Good news, otherwise that would have undone a whole load of my understanding of how laravel operates with the DB!!