I added a custom attribute to my model

public function getTouchedAttribute() { ...

I would like to add this to a query

hasMany()->where('touched', ...)

but obviously this isn't a column in the table.

what is the most elegant way to achieve this behavior?

推荐答案

一个选项(在性能方面可能更好)是用原始SQL函数模拟属性.(我帮不了你,因为我不知道touched能做什么)

The other way is to use filter on the resulting collection:

$collection = Model::all();
$filtered = $collection->filter(function($model){
    return $model->touched == true;
});

Laravel相关问答推荐

Laravel FAKER语法

Laravel 数据未传递到下拉框

Laravel:在行的子集上同步多对多

如何从laravel中的另一个表中获取用户名

如何避免谷歌翻译翻译:参数

Laravel 中的 index()" 是什么意思?

判断 Laravel 模型表中是否存在列,然后应用条件

Laravel:Blade foreach 循环 bootstrap 列

使用 Laravel 和 Passport 验证失败时响应状态码 401?

在表单中添加一对多 - Backpack laravel

将非框架 PHP 项目移植到 Laravel 4.x

何时在 Laravel 中使用 Repository vs Service vs Trait?

干预图像:直接从带有原始文件名和分机的网址保存图像?

Laravel 说 Auth guard [] 没有定义

laravel 4:更新行时如何从列的当前值中减一?

Laravel db 迁移 - renameColumn 错误 - 请求了未知的数据库类型枚举

在 Laravel 应用程序中在哪里指定应用程序版本?

在 Laravel 中按用户名查找用户

使用 Nginx 设置 Laravel

Laravel 4 简单路由无法使用 mod_rewrite 和 .htaccess