簡體   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