Laravel 管理插件,如何为每一行只调用一次函数
Laravel Admin plugin, how to call a function only once for each row
我有一个 Laravel 项目,上面有 Laravel 管理员 运行。
在“用户”页面上,我试图显示来自 users
table 和 pocket money
table 的信息,如下所示:
...
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use HerazikaServer\Models\User;
use HerazikaServer\Models\PocketMoney;
...
class UserController extends AdminController
{
protected function grid()
{
$grid = new Grid(new User());
// I'm trying to call this function only once for each row like so
// $current = PocketMoney::getCurrentMonthDetails($grid->id);
$grid->column('id', __('Id'));
$grid->column('nickname', __('Nickname'));
...
$grid->column('current pocket money period')->display(function() {
$current = PocketMoney::getCurrentMonthDetails($this->id);
return isset($current) ? $current->current_start_date.' '.$current->current_end_date : '';
})->style('min-width:100px');
$grid->column('daily objective')->display(function() {
$current = PocketMoney::getCurrentMonthDetails($this->id);
return isset($current) ? $current->daily_objective : '';
});
$grid->column('monthly objective')->display(function() {
$current = PocketMoney::getCurrentMonthDetails($this->id);
return isset($current) ? $current->monthly_objective : '';
});
$grid->column('attendance')->display(function() {
$current = PocketMoney::getCurrentMonthDetails($this->id);
return isset($current) ? $current->attendance.'/'.$current->monthly_objective : '';
});
...
return $grid;
}
}
但是我不得不为每一列调用 $current = PocketMoney::getCurrentMonthDetails($this->id);
(总共大约 20 次)。有没有办法只调用一次函数?
我尝试了一些类似 getCurrentMonthDetails($grid->id);
的方法。但它显然会引发未定义的错误。
您可以尝试这些解决方案:
1、从哪里启动功能,可以试试
$items = PocketMoney::query()->where('...')->limit($page,$limit)->all()->keyBy('id');
//..usage
$grid->column('monthly objective')->display(function() use($items) {
$current = $items[$this->id]
return isset($current) ? $current->monthly_objective : '';
});
2、调用$grid->column()的地方可以试试:
function loadPocketMoney($id){
if(!$item = cache()->get('product_'.$id)){
$item = PocketMoney::getCurrentMonthDetails($id);
cache()->set('product_'.$id,$item);
}
return $item;
}
$grid->column('monthly objective')->display(function() use($items) {
$current = cache()->get('PocketMoney_id')?:loadPocketMoney($this->id)
return isset($current) ? $current->monthly_objective : '';
});
我有一个 Laravel 项目,上面有 Laravel 管理员 运行。
在“用户”页面上,我试图显示来自 users
table 和 pocket money
table 的信息,如下所示:
...
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use HerazikaServer\Models\User;
use HerazikaServer\Models\PocketMoney;
...
class UserController extends AdminController
{
protected function grid()
{
$grid = new Grid(new User());
// I'm trying to call this function only once for each row like so
// $current = PocketMoney::getCurrentMonthDetails($grid->id);
$grid->column('id', __('Id'));
$grid->column('nickname', __('Nickname'));
...
$grid->column('current pocket money period')->display(function() {
$current = PocketMoney::getCurrentMonthDetails($this->id);
return isset($current) ? $current->current_start_date.' '.$current->current_end_date : '';
})->style('min-width:100px');
$grid->column('daily objective')->display(function() {
$current = PocketMoney::getCurrentMonthDetails($this->id);
return isset($current) ? $current->daily_objective : '';
});
$grid->column('monthly objective')->display(function() {
$current = PocketMoney::getCurrentMonthDetails($this->id);
return isset($current) ? $current->monthly_objective : '';
});
$grid->column('attendance')->display(function() {
$current = PocketMoney::getCurrentMonthDetails($this->id);
return isset($current) ? $current->attendance.'/'.$current->monthly_objective : '';
});
...
return $grid;
}
}
但是我不得不为每一列调用 $current = PocketMoney::getCurrentMonthDetails($this->id);
(总共大约 20 次)。有没有办法只调用一次函数?
我尝试了一些类似 getCurrentMonthDetails($grid->id);
的方法。但它显然会引发未定义的错误。
您可以尝试这些解决方案:
1、从哪里启动功能,可以试试
$items = PocketMoney::query()->where('...')->limit($page,$limit)->all()->keyBy('id');
//..usage
$grid->column('monthly objective')->display(function() use($items) {
$current = $items[$this->id]
return isset($current) ? $current->monthly_objective : '';
});
2、调用$grid->column()的地方可以试试:
function loadPocketMoney($id){
if(!$item = cache()->get('product_'.$id)){
$item = PocketMoney::getCurrentMonthDetails($id);
cache()->set('product_'.$id,$item);
}
return $item;
}
$grid->column('monthly objective')->display(function() use($items) {
$current = cache()->get('PocketMoney_id')?:loadPocketMoney($this->id)
return isset($current) ? $current->monthly_objective : '';
});