Php 如何按其他表中的记录筛选查询?-拉维尔

Php 如何按其他表中的记录筛选查询?-拉维尔,php,sql,laravel,Php,Sql,Laravel,这是我目前的问题,我想弄清楚 我有一个laravel查询,例如下面的例子 $users = User::where('country', $country) ->where('age', '>=' , $lfmin) ->where('age', '<=' , $lfmax) ->get(); return $users; 如何筛选

这是我目前的问题,我想弄清楚

我有一个laravel查询,例如下面的例子

$users = User::where('country', $country)

            ->where('age', '>=' , $lfmin)
            ->where('age', '<=' , $lfmax)
            ->get();                


            return $users;
如何筛选我的主查询,以使该查询中每个用户的id不在字段uone或utwo(用户1或用户2)下的datingblockeduser表中

编辑:我想实现一个类似于好友列表的阻止列表。我创建了一个新的迁移

Schema::create('blocked_user', function(Blueprint $table) {
        $table->increments('id');
        $table->integer('blocked_id')->unsigned()->index();
        $table->integer('user_id')->unsigned()->index();
        $table->foreign('blocked_id')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
在用户模型中,我做到了这一点

// blocked
public function blocked()
{
    $blocked = $this->belongsToMany('User', 'blocked_user', 'user_id', 'blocked_id');
    return $blocked;
}
public function block_user($blocked_id)
{
    $this->blocked()->attach($blocked_id);   // add friend
    $blocked = User::find($blocked_id);       // find your friend, and...
    $blocked->blocked()->attach($this->id);  // add yourself, too
}
public function remove_blocked($blocked_id)
{
    $this->blocked()->detach($blocked_id);   // remove friend
    $blocked = User::find($blocked_id);       // find your friend, and...
    $blocked->blocked()->detach($this->id);  // remove yourself, too
}

现在,我可以将其作为上述查询的一部分或以后的任何其他查询,以确保我只返回不在阻止列表中的用户吗?

为什么不查询所有阻止的用户id,而使用
whereNotIn()更改主查询

//通过身份验证的用户获取阻止的用户id。(自行改变条件)
$blockedId=Datingblockeduser::where('blocker_id',auth()->id())->pull('id');
$users=User::where('country',$country)
->其中NOTIN('id',$blockedId)
->其中('age','>=',$lfmin)

->where('age','为什么不查询所有被阻止的用户id,而使用
whereNotIn()更改主查询

//通过身份验证的用户获取被阻止的用户id。(自己更改where条件)
$blockedId=Datingblockeduser::where('blocker_id',auth()->id())->pull('id');
$users=User::where('country',$country)
->其中NOTIN('id',$blockedId)
->其中('age','>=',$lfmin)

->在哪里(‘age’,‘您需要在这两个表之间创建关系;多对多关系?因为据我所知,这是两个用户ID之间的多对多关系,但在同一个用户表中。是否有方法将datingblockedusers表用作同一表中关系的透视表?我猜一对一用户可以被阻止或删除不是吗?我决定多对多,因为阻止列表应该像朋友列表一样。相同类型的关系。请参阅我上面的编辑。你需要在这两个表之间创建关系;多对多关系?因为据我所知,这是两个用户ID之间的多对多关系,但在同一个用户表中。有没有方法使用e datingblockedusers表作为同一表中关系的透视表?一对一我猜用户可以被阻止还是不能?我决定多对多,因为被阻止的列表应该类似于好友列表。相同类型的关系。请参阅我上面的编辑。
// blocked
public function blocked()
{
    $blocked = $this->belongsToMany('User', 'blocked_user', 'user_id', 'blocked_id');
    return $blocked;
}
public function block_user($blocked_id)
{
    $this->blocked()->attach($blocked_id);   // add friend
    $blocked = User::find($blocked_id);       // find your friend, and...
    $blocked->blocked()->attach($this->id);  // add yourself, too
}
public function remove_blocked($blocked_id)
{
    $this->blocked()->detach($blocked_id);   // remove friend
    $blocked = User::find($blocked_id);       // find your friend, and...
    $blocked->blocked()->detach($this->id);  // remove yourself, too
}
// get blocked user id by authenticated user. (change where condition yourself)
$blockedId = Datingblockeduser::where('blocker_id', auth()->id())->pluck('id');

$users = User::where('country', $country)
            ->whereNotIn('id', $blockedId)
            ->where('age', '>=' , $lfmin)
            ->where('age', '<=' , $lfmax)
            ->get();