Fastadmin 后台表单,外键关键,步骤

本文详细介绍了在FastAdmin框架中实现关联查询的具体步骤,包括修改JS文件、控制器中添加index()方法并开启关联查询,以及在模型中定义关联方法。通过实际代码示例,展示了如何设置过滤方法、处理AJAX请求、构建参数、执行查询和返回结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在 public\assets\js\backend 目录中找到对应的js,修改

2.对应控制器中加上index()方法;开启关联查询

重点:

protected $relationSearch = true; //开启关联查询

public function index()
{

//设置过滤方法
$this->request->filter(['strip_tags']);
//当前是否为关联查询
$this->relationSearch = true; // 解决排序问题
if ($this->request->isAjax()) {
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->with(['choice'])
->where($where)
->order($sort, $order)
->count();

$list = $this->model
->with(['choice'])
->where($where)
->order($sort, $order)
->limit($offset, $limit)
->select();

$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);

return json($result);
}
return $this->view->fetch();
}

3.对应模型中model 中加上关联的方法

public function choice()
{
return $this->belongsTo('Choices', 'dw_id', 'id', [], 'LEFT')->setEagerlyType(0);   // Choices ------- 需要关联的模型  ;  dw_id ----- 外键id ; id ------ 关联表的id
}

 

4. 最终效果图

5.最后还有些小问题,为什么做了以上的步骤之后,只显示字段名,最后一步,同步语言包,把需要用的语言同步即可

 

 

 

详细文档,可查

https://doc.fastadmin.net/docs/controller.html#关联查询-5

转载于:https://www.cnblogs.com/roseY/p/9810397.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值