我想在自定義計算的布爾屬性上過濾一個背包列表。數據庫中沒有此屬性的列。我在模型中創建了屬性作為雄辯的訪問器。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 關注
- 168 瀏覽
添加回答
舉報
0/150
提交
取消