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

时间:2019-07-20 02:40:17

标签: php laravel backpack-for-laravel

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

datatable(some_value:string, d:dynamic) // just a sample data set with 2 records
[
    "hello", dynamic([
    {
        "name": "Data",
        "createOption": "Attach",
        "diskSizeGB": 10,
        "managedDisk": {
            "id": "/subscriptions/24ba3e4c-45e3-4d55-8132-6731cf25547f/resourceGroups/GREG/providers/Microsoft.Compute/disks/Data",
            "storageAccountType": "Standard_LRS"
        },
        "caching": "None",
        "toBeDetached": false,
        "lun": 0
    },
    {
        "name": "Disk2",
        "createOption": "Attach",
        "diskSizeGB": 10,
        "managedDisk": {
            "id": "/subscriptions/24ba3e4c-45e3-4d55-8132-6731cf25547f/resourceGroups/GREG/providers/Microsoft.Compute/disks/Disk2",
            "storageAccountType": "Standard_LRS"
        },
        "caching": "None",
        "toBeDetached": false,
        "lun": 1
    }
]), "world", dynamic([
    {
        "name": "Data3",
        "createOption": "Attach",
        "diskSizeGB": 10,
        "managedDisk": {
            "id": "/subscriptions/24ba3e4c-45e3-4d55-8132-6731cf25547f/resourceGroups/GREG/providers/Microsoft.Compute/disks/Data",
            "storageAccountType": "Standard_LRS"
        },
        "caching": "None",
        "toBeDetached": false,
        "lun": 0
    },
    {
        "name": "Disk23",
        "createOption": "Attach",
        "diskSizeGB": 10,
        "managedDisk": {
            "id": "/subscriptions/24ba3e4c-45e3-4d55-8132-6731cf25547f/resourceGroups/GREG/providers/Microsoft.Compute/disks/Disk2",
            "storageAccountType": "Standard_LRS"
        },
        "caching": "None",
        "toBeDetached": false,
        "lun": 1
    }
])
]
// --> answer starts here <--
| extend r = rand() // *
| mv-apply d on (   
    project d = pack("name", d.name, "storageAccountType", d.managedDisk.storageAccountType)
)
| summarize d = make_list(d) by r, some_value // *
| project-away r // *

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

+-------------------------------+
|  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); } ); 。我该如何解决?

非常感谢您!

1 个答案:

答案 0 :(得分:2)

您的用于生成员工代码列表的代码目前正在返回这样的数组,而程序包中则需要一个字符串数组。

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

要解决此问题,您需要从数组中pluck的{​​{1}}列中输入以下代码:

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);
    });