kfriars/searchable-select

Laravel Nova 字段。

1.3.2 2024-06-28 17:01 UTC

This package is auto-updated.

Last update: 2024-09-28 17:38:37 UTC


README

⚠️ 这是一个用于https://github.com/slovenianGooner/nova-searchable-select包的存档。此包将不会进行维护。⚠️

Laravel Nova 可搜索选择字段

一个用于 Laravel Nova 的可搜索选择字段。此字段结合了 BelongsTo 字段和 Select 字段的特性。

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

安装

Composer

composer require kfriars/searchable-select

用法

与常规选择字段用法相同,但您需要提供资源名称给 resource 方法而不是 options 方法。

use Kfriars\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()