【问题标题】:How to organize Laravel ORM for relationships table如何为关系表组织 Laravel ORM
【发布时间】:2020-07-30 23:46:47
【问题描述】:

我有两个表用户和关系。关系表包含下一个字段:

id | user_one_id (fk to users) | user_two_id (fk to users) | status | action_user_id

状态字段可以是 4 个值之一。 0 - 待处理,1 - 接受,2 - 拒绝,3 - 阻止。

操作用户是那些创建或更新请求的用户。例如,id 为 1 的用户想与 id 为 2 的用户成为朋友。action_user_id 将为 1。

我无法在我的用户模型中组织关系。

例如我的方法朋友

public function friends()
{
    return $this->belongsToMany(User::class, 'relationships', 'id', 'user_one_id')
                ->orWhere('user_two_id', $this->id);
}

创建下一个 sql 查询:

select * from "users" inner join "relationships" on "users"."id" = "relationships"."user_one_id" where "relationships"."user_one_id" = ? or "user_two_id" = ?

但我明白这不是我需要的。

我需要什么?我需要 3 个方法“朋友”、“请求”和“黑名单”。

“friends”方法必须返回当前用户的所有好友。 “requested”方法必须将所有好友请求返回给当前用户。并且“黑名单”必须返回当前用户处于被屏蔽状态的所有用户。

【问题讨论】:

  • 在关系表中,是不是像user_one_id的用户向user_two_id的用户发送请求一样?与The "requested" method must return all friend requests to current user. 一样,这意味着获取所有 user_two_id id,其中 user_one_id 属于给定用户并且请求状态?另外,如果你能用 user_one_id、user_two_id 来解释所有这些关系,那就太好了。
  • user_one_id 和 user_two_id 只是用户表的外键。至于“请求”,我的意思是从其他想要成为朋友的用户那里获得所有请求。
  • 假设这是来自关系表 user_one_id 1, user_two_id 6, status requested 的条目。现在这是什么意思,1 向 6 发送请求,或 6 向 1 发送请求?
  • 我忘了描述关系数据库中名为 action_user_id 的一个字段。我纠正它。
  • 假设 1 向 6 发送请求,那么 action_id 将为 1,如何将 1,6 分配给 user_one_id,user_two_id。我的意思是你应该将 1 分配给 user_one_id 还是 user_two_id?

标签: laravel eloquent


【解决方案1】:

尝试使用 where 子句,根据状态检索特定数据。我在全新的 laravel 安装中重新创建了该问题,并使用用户类和关系表来引用关系中的两个用户

//Model User: $user->friends gets the list of friends of the user
    public function friends()
    {
        return $this->belongsToMany('App\User', 'relationships','friend_2','friend_1')->where('friendship_status','friends');
    }

在blade.php 视图中:

@foreach(\App\User::all() as $user)
   <h5>{{$user->name}}</h5>
   @foreach($user->friends as $friends)
       <div>
            <small>Is friends with {{$friends->name}}</small>
       </div>
   @endforeach
@endforeach

关系表迁移:

Schema::create('relationships', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('friend_1');
            $table->unsignedbigInteger('friend_2');
            $table->string('friendship_status')->default('request pending');
            $table->timestamps();
        });

手动添加关系:

【讨论】:

  • 我认为查询中出现的那些join会返回不正确的结果
  • 究竟如何?你有测试来检查这些函数的输出吗?
  • 您可能需要在关系后面添加更多逻辑并在没有 orWhere 的情况下检索信息,例如: user1 hasblocked user2, user2_ isBlockedBy _user1 ,然后制作成对的函数,例如:function blockedBy() and function blocked()
  • 例如。我有一个 ID 为 1 的用户。我创建了两个新用户并将两条新记录添加到表关系中。现在我有两条记录 user_one_id = 1 | user_two_id = 2 | status_id = 1 和第二个 user_one_id = 1 | user_two_id = 3 | status_id = 1。在调用方法朋友后,我得到了一个空结果。为什么?因为这个属于不正确因为有一个join子句
  • 试试select * from "users" inner join "relationships" on "users"."id" = "relationships"."user_one_id" where status =3 --- return $this-&gt;belongsToMany('App\User', 'relationships', 'id', 'user_one_id')-&gt;where(status,'=',3);
猜你喜欢
  • 2017-07-24
  • 1970-01-01
  • 2015-01-21
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多