Laravel OrWhere 有两个条件不是那么明显

Laravel OrWhere having two conditions is not so obvious

为什么在 Eloquent 中一个简单的查询不起作用?

$matches = DB::table("matches")
   ->where(["team_a"=> $request->tid, "match_tipo"=>3])
   ->orWhere(["team_a"=> $request->tid, "match_tipo"=>3])
   ->first();

根据 Whosebug 上的其他示例,我应该在 Where 中使用查询,例如:

$matches = DB::table("matches")
 ->where(["match_tipo"=>3])
 ->where(function($query, $request) {
      $query->where('team_h',$request->tid)
            ->orWhere('team_a',$request->tid);
      })
->first();

但我应该传递第二个参数($request)。

最简单的查询方式是什么?

你可以这样做。

->where(["match_tipo"=>3])查询后为->where("match_tipo",3)use($tid)

$tid = $request->tid; 
$matches = DB::table("matches")
 ->where("match_tipo",3)
 ->where(function($query) use($tid) {
      $query->where('team_h',$tid)
            ->orWhere('team_a',$tid);
      })
->get();

在使用 Callback 或 Closure 作为 where 函数参数时,您将只有一个参数,即 Current对象

可用于传递 $request 的方法

Method One

$matches = DB::table("matches")
 ->where(["match_tipo"=>3])
 ->where(function($query) use ($request) {
      $query->where('team_h',$request->tid)
            ->orWhere('team_a',$request->tid);
      })
->first();

Method Two

$matches = DB::table("matches")
    ->where('match_tipo','=',3)
    ->when($request->tid,function($query) use ($request) {
        return $query->where('team_h',$request->tid)
              ->orWhere('team_a',$request->tid);
        })
   ->first();

试试这个查询:

$matches = DB::table("matches")
   ->where('match_tipo',3)
   ->whereRaw('team_a = "'.$request->tid.'" OR team_h = "'.$request->tid.'"')
   ->first();