我有两轮“第一轮和淘汰赛”。只有四支球队在第一轮获得最高分时才能进入淘汰赛。现在,我首先要从第一轮中选择那4个球队,然后检查输入的球队是否在所选的4个球队中。看一下我的代码(到目前为止,我已经尝试过了) [在这张图片中,标记为两支球队应该被排除在外,但是当我给出条件时,它并没有排除这两支球队]
$matches= new Match();
$matches->team1 = $request->input('team1');
$matches->team2 = $request->input('team2');
$ko = DB::select('SELECT * FROM points WHERE round="first" ORDER BY points DESC , run_rate DESC LIMIT 4');
if($ko == $matches->team1 || $ko == $matches->team2) {
$matches->round = "ko";
} else {
$matches->round = "first";
}
答案 0 :(得分:0)
首先,$ko
不包含查询结果,直到您将其传递给闭包为止,在这种情况下为->first()
。
$ko = DB::select('SELECT * FROM points WHERE round="first" ORDER BY points DESC , run_rate DESC LIMIT 4')->first();
接下来,您需要将$ko->team
的值与$matches->team1
或$matches->team2
进行比较:
if($ko->team == $matches->team1 || $ko->team == $matches->team2) {
...
}
最后,进行一些清理。可以简化数据库查询以使用Eloquent
语法,而不是原始的SELECT
:
$ko = DB::table("points")
->where("round", "=", "first")
->orderBy("points", "DESC")
->orderBy("run_rate", "DESC")
// ->limit(4) // Removing this; incompatible with `->first()`
->first();
还有另一个逻辑错误;如果需要limit(4)
,则不能使用->first()
,则必须使用->get()
,然后创建一个Collection
,无法比较到$matches
除非您循环:
$kos = DB::table("points")...->get();
$matches->round = "first";
foreach($kos AS $ko){
if($ko->team == $matches->team1 || $ko->team == $matches->team2) {
$matches->round = "ko";
break;
}
}
总而言之,您需要重新检查您要尝试做的事情并阅读Eloquent语法,如何执行查询和返回结果,如何循环,访问属性以及比较那些结果等。
编辑:由于要进行循环和比较,因此将$matches->round
的默认值设置为“ first”,然后在循环时,如果比较条件为true
,请覆盖$matches->round
进入“ ko”并跳出循环。