[Dcat Admin]表單無限極select

MTyu發表於2023-07-12

實現下圖:

【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 協議》,轉載必須註明作者和本文連結

相關文章