雄辩的搜索/自定义属性的位置 [英] eloquent search/where on custom attributes

查看:31
本文介绍了雄辩的搜索/自定义属性的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模型中添加了自定义属性

I added a custom attribute to my model

public function getTouchedAttribute() { ...

我想将此添加到查询中

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 的功能,所以无法为您提供帮助)

One option (and probably the better one in terms of performance) would be to mimic the attribute with raw SQL functions. (Can't help you with that because I don't know what touched does)

另一种方法是在结果集合上使用 filter :

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

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

这篇关于雄辩的搜索/自定义属性的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆