简体   繁体   English

Laravel whereIn在每个数组项上带有where子句

[英]Laravel whereIn with a where clause on each array item

Say I have a user object (which belongsToMany groups) and I'm doing a whereIn with an array of their respected ids like so: 假设我有一个用户对象(隶属于ToMany组),并且正在执行whereIn并使用其受尊重的ID数组,如下所示:

whereIn('user_id', $group->users->modelKeys())

I need to, however, set a condition where I only pull data from each array item based on a condition of the group_user pivot table, "created_at" (which is basically a timestamp of when that user was added to the group). 但是,我需要设置一个条件,仅根据group_user数据透视表“ created_at”的条件(基本上是该用户添加到组的时间戳)从每个数组项中提取数据。

So I need something like this: 所以我需要这样的东西:

whereIn('user_id', $group->users->modelKeys())->whereRaw('visits.created_at > group_user.created_at')

That doesn't work though because it's not doing the whereRaw for each array item but it's doing it once for the query as a whole. 但这不起作用,因为它没有为每个数组项执行whereRaw,而是为整个查询执行了一次。 I might need to do something like a nested whereIn but not sure if that'll solve it either. 我可能需要做一些嵌套的whereIn之类的事情,但不确定是否可以解决。 Thoughts? 有什么想法吗?

My full query as it is now: 我现在的完整查询是:

     $ids = $group->users->modelKeys();

     return DB::table('visits')->whereIn('user_id', function($query) use ($ids) {
        $query->select('user_id')->from('group_user')->whereIn('group_user.user_id', $ids)->whereRaw('visits.created_at > group_user.created_at');
    })->sum("views");

Ok got it to work using nested loops instead: 好吧,它可以使用嵌套循环来工作:

    $visits = DB::table('visits')->whereIn('user_id', $group->users->modelKeys())->get();

    $sum = 0;

    foreach($group->users as $user) {

        foreach($visits as $visit) {

            if($visit->user_id == $user->id) {

                if($visit->created_at >= $user->pivot->created_at) {
                    $sum += $visit->views;
                }
            }
        }

    }

    return $sum;

Would still like to see if it's possible to do it in a single query, no array looping. 仍然想看看是否有可能在单个查询中做到这一点,而没有数组循环。

Have you considered using a foreach ? 您是否考虑过使用foreach

$users = whereIn('user_id', $group->users->modelKeys());

foreach ($users as $user) {
  // do your comparison here
}

I guess you need to use JOINS for this query, following code may take you in right direction: 我想您需要为此查询使用JOINS,以下代码可能会带您正确的方向:

$ids = $group->users->modelKeys();

return DB::table('visits')->join('group_user', function ($query) use ($ids) {
    $query->on('visits.user_id', '=', 'group_user.user_id')
        ->whereIn('group_user.user_id', $ids)
        ->whereRaw('visits.created_at > group_user.created_at');
})->sum("views");

EDIT 编辑

$ids = $group->users->modelKeys();

return DB::table('visits')->join('group_user', function ($query) use ($ids) {
    $query->on('visits.user_id', '=', 'group_user.user_id');
})->whereIn('group_user.user_id', $ids)
   ->whereRaw('visits.created_at > group_user.created_at')->sum("views");

EDIT 编辑

$ids = $group->users->modelKeys(); $ ids = $ group-> users-> modelKeys();

return DB::table('visits')->join('group_user', function ($query) use ($ids) {
    $query->on('visits.user_id', '=', 'group_user.id') // group_user.id from group_user.user_id as per the loop
          ->on('visits.created_at', '>=', 'group_user.created_at');
})->whereIn('group_user.user_id', $ids)
   ->sum("views");

Solved it! 解决了! The foreach loop approach was making calls take waaaay too long. foreach循环方法使调用耗时太长。 Some queries had over 100k records returning (that's a lot to loop through) causing the server to hang up. 一些查询返回了超过10万条记录(要遍历很多条记录),导致服务器挂断。 The answer is in part a big help from Dharmesh Patel with his 3rd edit approach. 答案部分是Dharmesh Patel的第3种编辑方法所提供的巨大帮助。 The only thing I had to do differently was add a where clause for the group_id. 我要做的唯一不同的事情是为group_id添加了where子句。

Here's the final query (returns that 100k results query in milliseconds) 这是最终查询(返回以毫秒为单位的100k结果查询)

    //Eager loading. Has overhead for large queries
    //$ids = $group->users->modelKeys();

    //No eager loading. More efficient
    $ids = DB::table('group_user')->where('group_id', $group->id)->lists('user_id');

    return DB::table('visits')->join('group_user', function ($query) use ($ids) {
        $query->on('visits.user_id', '=', 'group_user.user_id')->on('visits.created_at', '>=', 'group_user.created_at');
    })->whereIn('group_user.user_id', $ids)->where('group_id', $group->id)->sum('views');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM