thinkphp5 model查询使用获取器

模型查询中使用withAttr获取器。

$data = [
       'key' => $this->request->get('key', '', 'trim'),
       'limit' => $this->request->get('limit', 10, 'intval'),
];
$list = TaskModel::withSearch(['url'], ['url' => $data['key']])
     ->withAttr('eid',function ($value,$data){
          return $this->getEngineName($value);
      })
    ->paginate($data['limit'], false, ['query' => $data]);

.
.
.
public function getEngineName($id){
   return EngineModel::get($id)->name;
}

使用方法如代码。调用function返回值。配合layui的table前台调用非常方便。如果不用layui,我可能会使用关联模型。

You May Also Like

About the Author: 萌新

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注