繁体   English   中英

Laravel背包中带有列的过滤器表

[英]Filter table with a column in Laravel Backpack

我有一个Employee表,显示如下:

+-------------------------------+
|  id  |    name    |   code    |
---------------------------------
|  1   | Employee 1 |    A1     |
|  2   | Employee 2 |    A2     |
| ...  |    ...     |   ...     |
+-------------------------------+

我想在此表中按代码列创建过滤器。 我的查询将是这样的:

SELECT name FROM employee WHERE code LIKE .% $filter %.

我在背包文件中搜索并尝试这样做

$this->crud->addFilter(
    [
        'type' => 'select2',
        'name' => 'code',
        'label' => 'Filter',
    ],
    function () {
        return Employee::select('code')->distinct()->get()->toArray();
    },
    function ($value) {
        $this->crud->addClause('where', 'code', $value);
    }
);

但出现错误: htmlspecialchars() expects parameter 1 to be string, array given 我该如何解决?

非常感谢你!

您的用于生成员工代码列表的代码此刻正在返回这样的数组,而程序包则期望一个字符串数组。

[
    ['code' => 'A1'],
    ['code' => 'A2'],
];

要解决此问题,您需要从数组中pluck code列,并通过代码对其进行键入:

$this->crud->addFilter([
        'type' => 'select2',
        'name' => 'code',
        'label' => 'Filter',
    ],
    function() {
        return Employee::select('code')->distinct()->get()->pluck('code', 'code')->toArray();
    },
    function($value) {
        $this->crud->addClause('where', 'code', $value);
    });

这将导致类似:

[
    'A1' => 'A1',
    'A2' => 'A2',
];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM