familycare/searchable-select

Laravel Nova 字段。

1.3.2 2024-06-30 05:56 UTC

This package is not auto-updated.

Last update: 2024-09-23 05:30:21 UTC


README

为 Laravel Nova 提供的搜索选择字段。该字段结合了 BelongsTo 字段和 Select 字段的特性。

基本上是一个常规选择字段,您指定要搜索的资源,不需要“关系”。这意味着,您也可以在数据库的额外 JSON 字段中使用它。

安装

Composer

composer require sloveniangooner/searchable-select

使用方法

与常规选择字段类似,但您提供的是 resource 方法,而不是 options 方法,并传递您的资源名称。

use Sloveniangooner\SearchableSelect\SearchableSelect;
...

SearchableSelect::make('Content', 'content_id')->resource("contents")
... or
SearchableSelect::make("Content", "content_id")->resource(\App\Nova\Content::class)

您可以使用所有常规选项,如

SearchableSelect::make('Content', 'content_id')
    ->resource("contents")
    ->help("Help text")
    ->displayUsingLabels()
    ->nullable()

但也可以添加三个附加选项

SearchableSelect::make('Content', 'content_id')
    ->resource("contents")
    ->label("custom_label_field") // Defaults to the static $title attribute of the resource class
    ->labelPrefix("custom_prefix_field") // Allows you to prefix the label field with one other field, i.e. "code":"label"
    ->value("custom_value_field") // Defaults to 'id'

现在您也可以选择多选选项。需要数据库中的 textjson 字段。

SearchableSelect::make('Content', 'content_id')
    ->resource("contents")
    ->multiple()
    ->displayUsingLabels()
    ->nullable()

另一个选项是定义搜索中显示的项目最大数量。(默认:20)

SearchableSelect::make("Content", "content_id")
                ->resource("contents")
                ->max(10)

您可以使用基模型的搜索方法而不是 Nova 资源模型的搜索方法,通过使用 useBaseSearch()

SearchableSelect::make('Content', 'content_id')
    ->resource("contents")
    ->useBaseSearch()