如何根据辅助函数的返回值在laravel查询中创建条件

我正在使用Laravel 5.7。我有一个“匹配”模型,其第一个参与者名称是从帮助函数getFirstParticipant(Match $ match)返回的。 我正在尝试从DB的某些参与者中获取某些匹配项,例如'约翰'。我是否有可能使用雄辩的查询功能来做到这一点?

Match::where('firstParticipant','John')

或任何其他解决方案?

如果要帮助您声明问题,我在下面复制我的辅助函数:

function getFirstParticipant(Match $match)
    {
        $structure_id = $match->structure_id;
        $seed = $match->matchResult->first_seed;

        $placement = Placement::where('structure_id',$structure_id)->where('seed',$seed)->first();

        return !empty($placement->player_id) ? $placement->player->username : $placement->team->name;
    }
jinmingh 回答:如何根据辅助函数的返回值在laravel查询中创建条件

只需将变量包含在第二个参数中即可。

$first = getFirstParticipant(Match $match);

Match::where('firstParticipant',$first->first_name)->get();

很明显,我在这里假设了一些变量名,例如first_name。但是您应该能够遵循。只需使用您的帮助程序即可返回模型值,然后将该返回值作为where参数的第二个参数插入。

,

没有,没有办法。除了数据库设计问题之外,我看到此工作的唯一方法是使用collection方法。

Match::get()->filter(function ($match) {
    return $match->firstParticipant == 'John'; // Or is it getFirstParticipant($match) ?
})->all();
# Laravel 6.x introduces LazyCollections
Match::cursor()->filter(function ($match) {
    return $match->firstParticipant == 'John'; // Or is it getFirstParticipant($match) ?
})->all();
本文链接:https://www.f2er.com/3165020.html

大家都在问