peterLee

peterLee

[Dcat Admin]表單無限級select

贴上代码:

<?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 $v;
    })->required();

//篩選器中
$filter->equal('category_id')->select(function () {
  return GoodsCategory::selectOptions();
});
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。