FastAdmin a表用with关联b表后,再使用后台通用搜索(加入b表的搜索条件)报错

问题

我用user表关联了userCount表

 

但是,此时使用FastAdmin后台的通用搜索,设置userCount表的搜索条件,会报错

Unknown column 'user_count.sum_deposit' in 'where clause'

 

解决经过

通过看 F12的Network发现 ,sql语句中的错误

好像是自动把别名转义为驼峰式的 `userCount`,但是我发起请求传过去的是  `user_count`,

于是我尝试将别名改过来,自己给他起别名

第一次

$this->belongsTo中的第四个参数本来是起别名的,但是发现给废弃掉了

第二次

我使用->alias(['userCount' => 'user_count'])

但是报错又变成了这样

 

 

 

解决方案

将返回的数据的 `user_count` 改为 `userCount` ,这样的话就一直使用驼峰式的表别名,使用后台通用搜索时传递过来的也就是 `userCount.sum_deposit` 了

 1     $list = $this->model
 2         ->with(['group', 'userCount'])
 3         ->where($where)
 4         ->order($sort, $order)
 5         ->paginate($limit);
 6     foreach ($list as $k => $v) {
 7         $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
 8         $v->hidden(['password', 'salt']);
 9     }
10     // 这段是改变返回值的`user_count`为`userCount`
11     $items = $list->items();
12     foreach ($items as &$v) {
13         $v->userCount = $v->user_count;
14         unset($v->user_count);
15     }

 

posted @ 2021-01-22 16:45  九鹤  阅读(2363)  评论(0编辑  收藏  举报