[Dcat Admin]表单无限极select

实现下图:

【Dcat Admin】表单无限极select

GoodsCategory代码

<?php


namespace App\Models;


use Dcat\Admin\Traits\HasDateTimeFormatter;
use Dcat\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class GoodsCategory extends Model
{
    use ModelTree; //必须
    use HasDateTimeFormatter;
    use SoftDeletes;

    protected $titleColumn = 'name'; //如果表中分类名称字段名不是‘title’
    protected $orderColumn = 'sorts'; //如果表中排序字段名不是‘order’

    public static function getAll ()
    {
        return self::where('status', 1)->whereNull('deleted_at')->latest('sorts')->get(['id', 'name', 'parent_id']);
    }

    public static function selectOptions(\Closure $closure = null)
    {
        $options = (new static())->withQuery($closure)->buildSelectOptions();

        return collect($options)->all();
    }
}

GoodsController中使用

//表单中
$form->select('category_id')
                ->options(GoodsCategory::selectOptions())
                ->saving(function ($v) {
                    return (int) $v;
                })->required();

//筛选器中
$filter->equal('category_id')->select(function () {
  return GoodsCategory::selectOptions();
});
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 7
yangweijie

tp以前项目就这么做的,最近有这种需求,用的select2 实现,但是转义了,目前动态先渲染html后初始化的

9个月前 评论
yangweijie

buildSelectOptions 没有放出来

9个月前 评论

这种分类在做搜索的时候好做吗?感觉不太好做

9个月前 评论
DogLoML 7个月前

请问这样怎么把id设置为对应的键名呢 我在这出了问题 $options值的键名键值和数据库的id对应不上

7个月前 评论
DogLoML

saving里面不用转int吧,不然(int)null会转成0,感觉rules里面验证数值类型好一点

7个月前 评论
肌无力猛男 7个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!