Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何阻止';跟随者';拉维尔_Php_Laravel - Fatal编程技术网

Php 如何阻止';跟随者';拉维尔

Php 如何阻止';跟随者';拉维尔,php,laravel,Php,Laravel,我已经创建了使用透视表和多对多关系在laravel应用程序中添加和删除朋友的功能 我现在陷入了处理阻止一个成员与另一个成员建立友谊的最佳方式 现在我有一个带有时间戳的“阻塞”表,阻塞用户和创建人 最好的检查方法是在添加好友之前检查块表中是否存在关系?方法可能如下: $blocked_user = DB::table('blocked') ->where([ ['created_by', '=', $user_id], ['blocked_user', '=', $block

我已经创建了使用透视表和多对多关系在laravel应用程序中添加和删除朋友的功能

我现在陷入了处理阻止一个成员与另一个成员建立友谊的最佳方式

现在我有一个带有时间戳的“阻塞”表,阻塞用户和创建人


最好的检查方法是在添加好友之前检查块表中是否存在关系?

方法可能如下:

$blocked_user = DB::table('blocked')
->where([
    ['created_by', '=', $user_id],
    ['blocked_user', '=', $blocked_user_id],
])
->first();

# user isn't blocked
if (empty($blocked_user)) {
    # add friend procedure
}

假设你在使用模型。并且您的
用户
模型与
blockedUsers
有关系: 编辑:


只要在你认为合适的地方调用这个方法。您可以从
addFriend
调用它来检查此用户是否被阻止。

我非常喜欢这种方法!我只是因为某种原因迷路了什么是被封锁的关系。“会是一对多吗?”沃克·基恩说,会是多对多。我刚刚编辑了代码。非常感谢你在这方面的帮助,我不知道我为什么要空白。
User extends Model {
    // ...
    public function isBlocked($userId)
    {
        return (boolean) $this->blockedUsers()
            ->where('blocked_user_id', $userId)->count();
    }
    public function blockedUsers()
    {
        return $this->belongsToMany(User::class, 'blocked_users_table', 'user_id', 'blocked_id');
    }
}