peterLee

peterLee

[Dcat Admin] Infinite select in forms

Paste the code:

<?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; //required
    use HasDateTimeFormatter;
    use SoftDeletes;

    protected $titleColumn = 'name'; //if the field name of the category name in the table is not 'title'
    protected $orderColumn = 'sorts'; //if the field name of the sort in the table is not '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();
    }
}

// Used in GoodsController
// In the form
$form->select('category_id')
    ->options(GoodsCategory::selectOptions())
    ->saving(function ($v) {
        return $v;
    })->required();

// In the filter
$filter->equal('category_id')->select(function () {
  return GoodsCategory::selectOptions();
});
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.