Tom*_*ato 3 php laravel laravel-backpack
我有一个员工表,显示如下:
+-------------------------------+
| id | name | code |
---------------------------------
| 1 | Employee 1 | A1 |
| 2 | Employee 2 | A2 |
| ... | ... | ... |
+-------------------------------+
Run Code Online (Sandbox Code Playgroud)
我想在此表中按代码列创建一个过滤器。我的查询将是这样的:
+-------------------------------+
| id | name | code |
---------------------------------
| 1 | Employee 1 | A1 |
| 2 | Employee 2 | A2 |
| ... | ... | ... |
+-------------------------------+
Run Code Online (Sandbox Code Playgroud)
我在背包文档中搜索并尝试这样做
SELECT name FROM employee WHERE code LIKE .% $filter %.
Run Code Online (Sandbox Code Playgroud)
但出现错误:htmlspecialchars() expects parameter 1 to be string, array given。我该如何解决这个问题?
非常感谢!
用于生成员工代码列表的代码目前正在返回一个像这样的数组,而包需要一个字符串数组。
[
['code' => 'A1'],
['code' => 'A2'],
];
Run Code Online (Sandbox Code Playgroud)
要解决此问题,您需要从数组中获取列,并通过代码键入它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);
});
Run Code Online (Sandbox Code Playgroud)
这将导致类似的结果:
[
'A1' => 'A1',
'A2' => 'A2',
];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4106 次 |
| 最近记录: |