我有一个员工表,显示如下:
+-------------------------------+
| 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
the 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',
];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)