【问题标题】:Find conversation thread between 2 users excluding group conversations查找 2 个用户之间的对话线程,不包括群组对话
【发布时间】:2018-11-24 08:21:55
【问题描述】:

我有一个基本的私人消息系统,能够进行一对一对话和群组对话。

目前我在网站上有一个“发送消息”小部件框,用户可以在其中输入用户名并向他们发送消息,从而开始“一对一”对话线程。

我需要这个小部件做的是,当发送消息时,检查两个用户之间的对话是否已经存在,如果存在,则将新消息添加到已经存在的对话线程中。如果对话不存在,则开始一个新线程。

我的主要问题是,如何排除检查两个用户都是参与者的对话线程。

2个相关表的sn-p如下:

线程表:

  • 标题
  • 类型

thread_participants 表

  • thread_id
  • user_id

话题“类型”确定话题是“单人”(1-1 对话)还是“小组”对话。

我的理论是在线程表中搜索两个用户在同一个线程中的位置,但是我不确定如何在控制器中使用 eloquent 来做到这一点。

希望这很清楚。谢谢

【问题讨论】:

    标签: php laravel laravel-5 eloquent


    【解决方案1】:

    您可以从threads表中选择您的相关信息并将其加入thread_participants并为表thread_participants命名(例如,作为new_thread_participants)并再次加入它。然后,您可以根据自己的条件指定限制器

    DB::table('threads')
    ->select('some_columns')
    ->join('thread_participants', 'threads.id', '=', 'thread_participants.thread_id')
    ->join('thread_participants AS new_table', 'threads.id', '=', 'new_table.thread_id')
    ->where('threads.type', 'single')
    ->where('thread_participants.user_id', user_1_id)
    ->where('new_table.user_id', user_2_id)
    ->get();
    

    这也可能有助于https://www.w3schools.com/sql/sql_alias.asp

    【讨论】:

      【解决方案2】:

      在 Laracast 论坛的帮助下,我想出了这个:

      $threadBetweenTwoUsers = Thread::whereIn('id', $userParticipant)->where('type', 'single')
      ->whereHas( 'participants', function($query) use ($recipient_id){ 
          $query->where('user_id', $recipient_id); // filter by other participant
      })
      ->first();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-11
        • 1970-01-01
        • 1970-01-01
        • 2018-04-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多