首页 > 解决方案 > 在 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);

非常感谢

标签: phplaraveleloquent

解决方案


由于您使用集合,您可以使用filter方法:
https ://laravel.com/docs/8.x/collections#method-filter


推荐阅读