[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 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 7
yangweijie

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

1年前 评论
yangweijie

buildSelectOptions 没有放出来

1年前 评论

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

1年前 评论
DogLoML 1年前

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

1年前 评论
DogLoML

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

1年前 评论
肌无力猛男 1年前

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