首页 > 解决方案 > 如何捕获在聊天中发送消息的用户

问题描述

我无法显示已向其他用户发送消息的用户。请参阅下文以了解我的意思:

我的表消息:

Schema::create('messages', function (Blueprint $table) {
            $table->id();
            $table->integer('de_user_id')->unsigned();
            $table->foreign('de_user_id')->references('id')->on('users');
            $table->integer('para_user_id')->unsigned();
            $table->foreign('para_user_id')->references('id')->on('users');
            $table->text('message');
            $table->timestamps();
        });

我有一个页面,它将显示已向其他成员发送消息的用户:

<section class="conversations">
                <a href="https://www.example.com/profile/messages/username" class="conversation ">
                    <div class="info">
                        <div class="name ">
                            Marcos
                        </div>
                    </div>
                    <div class="date"><span aria-hidden="true" class="fas fa-circle"></span> 3 months ago
                    </div>
                </a>
  </section>

但是要显示谁发送了消息,我需要在表格中访问id谁收到了消息。Message另外,我需要获取发送它的用户。我的问题是我不知道如何识别谁曾经发送过它。这样我就得到了所有消息的集合。

这是我的做法:

public function msgs(Request $request) {

      $msg = Message::where('para_user_id', $request->user()->id)->get();

      return view('msgs',compact('msg'));
        
    }

但是我怎样才能收到来自同一个用户的多条消息。此解决方案不起作用。无论发送了多少条消息,我都需要捕获一个向我发送消息的时间。所以,只返回给我发消息的用户。

你能帮助我吗 ?

标签: laravel

解决方案


以下应该工作

    $msg = Message::where('para_user_id', $request->user()->id)->get()->unique('de_user_id')->values();


推荐阅读