php - 在 Laravel 查询中使用模型函数
问题描述
我该如何解决这个问题
这是用户模型:
public function scopeHasEnteredTournament($query){
$active = Order::all()
->where('user_id','=',$this->id)
->where('status','=',1)
->where('pack_id','=',3)
->where('expired_at','>',now())
->first();
$tournament = Tournament::all()
->where('status','=',1)
->where('start_at','<',now())
->where('end_at','>',now())
->first();
if($active && $tournament){
return true;
}
return false;
}
这是我的控制器代码:
$all = User::all()
->sortByDesc('tournamentPoints')
->where('hasEnteredTournament')
->take(200);
非常感谢
解决方案
由于您使用集合,您可以使用filter
方法:
https ://laravel.com/docs/8.x/collections#method-filter
推荐阅读
- python - PySpark GC 开销限制超出并超时
- mongodb - 如何每分钟向 MongoDB 插入大量数据(约 100 万条记录)?
- r - seq.default(1, length(tokens), 5000) 中的错误:R 中的“by”参数中的错误符号
- javascript - 删除公共路径时,Laravel 6 混合路径不起作用
- python - 如果在 Python 中找到空列表,则删除字典
- reactjs - 根据输入值禁用链接:ReactJS
- php - 如何将区域语言 php 数组数据(古吉拉特语、印地语、旁遮普语)存储和检索到 mysql 数据库?
- python - 具有确定顺序的 Pandas One-Hot-Encoding
- amazon-web-services - 如何创建正确的 S3 存储桶策略以启用对文件的读取访问权限,前提是他们知道路径
- git - 如何强制 git diff 创建 git 二进制补丁?