我想在自定义计算的布尔属性上过滤一个背包列表。数据库中没有此属性的列。我在模型中创建了属性作为雄辩的访问器。public function getCalculatedBooleanAttribute($value){ $bool = // get result of calculation as true/false return $bool;}然后我可以在我的控制器中使用以下内容将值显示为一列。$this->crud->addColumn('calculated_boolean');到目前为止,这非常有效,我可以在列表视图中看到预期的结果。我现在需要过滤这个视图......我尝试过使用 addClause 的标准 crud 过滤器,但这给出了有关缺少数据库字段的异常。例如$this->crud->addClause('where', 'calculated_boolean', '1'); Column not found: 1054 Unknown column 'calculated_boolean' in 'where clause'我在文档中找不到任何显示如何过滤此属性的内容。任何人都可以建议一种过滤数据库中没有列的字段的方法吗?
1 回答

江户川乱折腾
TA贡献1851条经验 获得超5个赞
的确。您的访问器只是一个 PHP 函数,它以某种方式操作 MySQL 响应。无论你在 Laravel 访问器中做什么,都会在你的数据库响应之后发生。所以你不能在 Eloquent 查询中添加访问器。
您可能可以在 addFilter 闭包(第三个参数)中使用whereRaw将该列添加到您的查询中:
$this->crud->addFilter([ // simple filter
'type' => 'simple',
'name' => 'active',
'label'=> 'Active'
],
false,
function() { // if the filter is active
$this->crud->query = $this->crud->query->whereRaw('price * ? as calculated_boolean', [1.0825]);
$this->crud->addClause('where', 'calculated_boolean', '1');
} );
注意你可以操纵$this->crud->queryt
- 1 回答
- 0 关注
- 171 浏览
添加回答
举报
0/150
提交
取消