Mysql 如何在laravel中组合四个查询?

Mysql 如何在laravel中组合四个查询?,mysql,laravel,Mysql,Laravel,我在我的网站上有抽签,为了参加抽签,你需要每天做一些特定的动作。有一个代码可以检查所有这些: $date = Carbon::today(); $sta = \DB::table('ets')->where('user_id', $this->user->id)->where('created_at', '>=', $date)->get(); $sta = \DB::table('ets_1x1')->where('user_i

我在我的网站上有抽签,为了参加抽签,你需要每天做一些特定的动作。有一个代码可以检查所有这些:

    $date = Carbon::today();
    $sta = \DB::table('ets')->where('user_id', $this->user->id)->where('created_at', '>=', $date)->get();
    $sta = \DB::table('ets_1x1')->where('user_id', $this->user->id)->where('created_at', '>=', $date)->get();
    $sta = \DB::table('ets_low')->where('user_id', $this->user->id)->where('created_at', '>=',$date)->get();
    $sta = \DB::table('ets_duel')->where('user_id', $this->user->id)->where('created_at', '>=', $date)->get();

if ($sta == NULL) {
                return response()->json(['status' => 'error', 'msg' => 'Error']);
            }
此代码检查4个可能的表中是否有用户记录。我在表
ets_1x1
中输入了一个条目,但仍然无法参与,因为数据库中似乎找不到我。我删除了所有表格,只留下了
ets_1x1
,我被纳入了图纸

据我所知,该值取自上一个请求。如何将查询合并为1并对这4个表进行检查

UPD:

我还尝试给变量赋予新的名称,并以不同的方式显示响应代码,现在所有人都可以参与绘图,即使是那些未满足条件的人,现在看起来:

    if(!empty($sta_1) || !empty($sta_2) || !empty($sta_3) || !empty($sta_4)) {
        return response()->json(['status' => 'error', 'msg' => 'Error']);
    }

我的错误在哪里?

该代码无法工作,因为:

  • 第一段代码将只计算最后一个请求(并且是连续的,仅当最后一个表上存在任何用户时)
  • 第二段代码的计算结果不正确,您正在Laravel集合上运行空函数
  • 你为什么不试试这个?我认为它应该起作用:

    $date = Carbon::now();
    $userExists = false;
    $tables = ['ets', 'ets_1x1', 'ets_low', 'ets_duel'];
    
    foreach ($tables as $tableName) {
        $result = \DB::table($tableName)
            ->where('user_id', $this->user->id)
            ->where('created_at', '>=', $date)
            ->get()
        ;
        if ($result->isNotEmpty()) {
            $userExists = true;
            break;
        }
    }
    
    if (!$userExists) {
        return response()->json(['status' => 'error', 'msg' => 'Error']);
    }
    

    您正在为同一变量赋值,而不是将其合并覆盖使用join all Table您的意思是使用第一个变量,但是在
    ->get
    之后使用
    ->join
    ?所有表都有用户id,所以只有在第一个表中有where条件,其余的表在ets上有join。用户id=ets\u 1x1。用户id等等我仍然不清楚你需要什么?你需要这四个表的数据吗?看起来你和其他四个表有一对多的关系