laravel-admin表单提交两级联动功能编写
Laravel  /  管理员 发布于 3年前   4025
laravel-admin表单提交两级联动功能编写;
控制器:
1.
namespace App\Admin\Controllers;
use App\Http\Controllers\Controller;
use App\Models\SelectOption;
use App\Models\ProductCreditCard;
use App\Models\Top;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
2.
public function form()
{
$form = new Form(new Top());
//这里就是我的两级联动
$form->select('bankid','银行')->options(
SelectOption::companycreditcards()
)->load('cid', '/api/top/cid');//选中传入接口一个q参数,加载到第二级
//加上需options不然编辑页不会选中
$form->select('cid','信用卡')->options(function ($id) {
return ProductCreditCard::where('id',$id)->pluck('name', 'id');
});
$form->select('ctype','类别')->options(SelectOption::categoryOps([1591,1599,1608,1621]));
$form->display('created_at');
$form->display('updated_at');
//$form->switch('status', '状态')->states($this->states);
return $form;
}
3.加载的接口(我也写在这个控制器里面--->需写路由过来)
public function cid(Request $request)
{
$id = $request->get('q');
return ProductCreditCard::where('CompanyID', $id)->get(['id', DB::raw('name as text')]);
}
路由:
//选中加载返回数据
$router->get('api/top/cid', 'TopController@cid');
看看效果:
针对评论留言中@lin的问题:第二级分类编辑如果要想选择该父类下的其他子类
稍作修改代码如下:
$form->select('bankid','银行')->options(
SelectOption::companycreditcards()
)->load('cid', '/api/top/cid');
$form->select('cid','信用卡')->options(function () {
//获取父类id
$fid = $this -> bankid;
//查询出父类下的所有子类
$return = ProductCreditCard::where('CompanyID',$fid)->pluck('name', 'id');
return $returm;
});
路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..博主 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 @ mashrdn 多切换几个节点测试,免费ssr是没那么稳..mashrdn 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 V2rayn免费节点添加上去了,youtobe无法打开网页,是怎么回事..张伟 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 3q!有用,不过免费节点隔天就要去git上复制新的导进去..博主 在
科学上网翻墙访问Google , 上外网神器佛跳墙VPN(永久免费)使用流程步骤中评论 该篇教程已不能用了,告知大家,免的老有老铁问我!..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号