我向模型添加了自定义属性
public function getTouchedAttribute() { ...
我想将此添加到查询中
hasMany()->where('touched', ...)
但这显然不是表中的一列。
实现此行为的最优雅方法是什么?
最佳答案
一种选择(就性能而言可能是更好的选择)是使用原始SQL函数来模仿属性。 (因为我不知道touched
的作用,所以无法为您提供帮助)
另一种方法是在结果集合上使用filter
:
$collection = Model::all();
$filtered = $collection->filter(function($model){
return $model->touched == true;
});
关于laravel - Eloquent 搜索/自定义属性的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28020053/