tasdan/column-searchable

Laravel中按列搜索的包

0.6 2023-03-17 13:16 UTC

This package is not auto-updated.

Last update: 2024-09-27 20:30:45 UTC


README

一个用于在Laravel中搜索Eloquent模型列的包,带有关系。

Total Downloads Latest Stable Version Latest Unstable Version License

目录

安装

Composer

使用composer require命令安装包

composer require "tasdan/column-searchable"

服务提供者

接下来,将新的提供者添加到config/app.php文件中的providers数组中(仅当Laravel < 5.5时)

'providers' => [
    // ...
    /**
     * Third Party Service Providers...
     */
    Tasdan\ColumnSearchable\ColumnSearchableServiceProvider::class,
    // ...
],

配置

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Tasdan\ColumnSearchable\ColumnSearchableServiceProvider" --tag="config"

之后,您可以在config/columnsearchable.php文件中更改默认配置。

用法

要使用可搜索作用域,您必须在Eloquent模型内部添加Searchable特质。 Searchable特质为模型添加可搜索作用域。然后,在模型定义中定义$searchable数组(有关更多信息,请参阅配置部分)

use Tasdan\ColumnSearchable\Searchable;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword, Searchable;
    // ...

    public $searchable = [
        'search_field_name_1' => [
            'relation' => 'relation_table_name',
            'columns' => 'column_name',
            'type' => 'string/int'
        ],
        'search_field_name_2' => [
            'relation' => 'relation_table_name',
            'columns' => 'column_name',
            'type' => 'string/int'
        ],
    // ...
}

定义了$searchable数组后,您可以在控制器中使用searchable作用域

 $users = User::searchable()->paginate(15);

当请求的URL包含定义的$searchable键之一时,您可以执行搜索函数,请参阅此示例

https://yourdomain.com/users?search_field_name_1=test%20user

注意:URL中的参数值是URL编码的。

配置

您可以使用以下模式配置搜索字段,您想配置多少就可以配置多少

  • search_field_name_1:此搜索字段的名称,可以是任何名称,也可以是您想要搜索的列名
  • relation:相关表名,可以省略,也可以是当前模型的表名
  • columns:您想要搜索的列名,使用数组,当您想要在多个列中搜索时
  • type:列类型,必须是字符串或int(如果是字符串,则查询类似于'LIKE %xyz%'),可以省略

配置示例

 public $searchable = [
        //search in name column in current table with string type 
        'name',

        //search in selected columns with OR relation in current table with string type
        'address' => [
            'columns' => [
                'country',
                'city',
                'zip_code',
                'street'
            ]
        ],
        
        //search in activation_code column in current table with string type, but the search field name is activation
        'activation' => [
            'columns' => 'activation_code'
        ],
       
        //search in is_active column in current table with int type
        'is_active' => [
            'type' => 'int'
        ],
        
        //search in a belongs-to related table's name column, with string type
        'school_name' => [
            'relation' => 'school',
            'columns' => 'name',
            'type' => 'string'
        ],
    
        //search in a belongs-to related table's city and street column with string type'
        'school_address' => [
            'relation' => 'school',
            'columns' => [
                'city',
                'street'
            ],
            'type' => 'string'
        ],   

        //search in a has-many related table's firstname and lastname column with string type
        'student_name' => [
            'relation' => 'students',
            'columns' => [
                'firstname',
                'lastname'
            ],
            'type' => 'string'     
        ]     
    ];

Blade 扩展

您可以使用以下两种Blade扩展来使用可搜索函数

可搜索输入字段

在Blade文件中,您可以使用@searchablefield扩展来自动生成搜索输入表单。

@searchablefield('field_type', 'search_field_name', 'Title', $selectOptionsArray, ['class' => 'form-control'])

注意:自动添加了搜索-input类到生成的字段,以使@searchablescript扩展能够工作。

第一个参数(field_type)必须是以下之一

  • text
  • select

第二个参数是可搜索字段名称,这已在模型中的$searchable数组中定义。第三个参数是字段的标题占位符值。第四个参数取决于类型参数

  • 如果类型是text,这是一个包含附加表单值的数组(可选)
  • 如果类型是select,这是一个包含选择选项键值对的数组

当类型是select时,第五个参数是可选的,这是一个包含附加表单值的数组。

此Blade扩展的可能示例和用法

@searchablefield('text', 'name', __('fields.name'))
@searchablefield('text', 'location', __('fields.location'), ['class' => 'form-control form-control-sm'])
@searchablefield('select', 'is_active', __('fields.active'), $activeSelectOptions)
@searchablefield('select', 'position', __('fields.position'), ["1" => "POS 1", "2" => "POS 2"] ,['class' => 'form-control'])

可搜索脚本

在Blade文件中,您可以使用@searchablescript扩展来自动生成搜索JavaScript函数。之后,您可以调用search函数来执行搜索请求。

@searchablescript扩展的示例用法

<script>
    @searchablescript()
</script>
// ...
<script>
    $("#searchButton").click(function () {
        search();
    });
</script>

注意:您必须在script标签内部调用@searchablescript扩展,因为此扩展仅返回JS函数代码。这样,您就可以在包含中使用CSP-nonce。

分页

如果您在Blade中使用分页,则必须更新分页链接,以在页面更改时保持搜索参数在URL中。

{!! $users->appends(\Request::except('page'))->render() !!}

完整示例

待办事项

库注释

这是我第一个Laravel包,可能存在一些问题,但我将尽快处理。

许可证

本软件包是免费软件,根据MIT许可证条款分发。祝您使用愉快!