如何使用 laravel 8 显示发件人的最后一条消息?

How can i show sender's last message using laravel 8?

我正在使用 laravel 和 livewire 构建一个简单的私人聊天应用程序。

我有一条消息 table,其中有一列:id/sender_id/receiver_id 和正文

我正在尝试向收件人显示最后的发件人消息,但不幸的是我遇到了错误 https://flareapp.io/share/B5Z18q07#F83 请帮助我如何解决这个问题?谢谢你

用户模型

public function messages(){

        return $this->hasMany(Message::class);
    }

app\Http\Livewire\Messaging.php

class Messaging extends Component
{
    public $body;
    public $searchTerm;
    public $selectedUser;

    public function mount(){

            $this->selectedUser =User::where('id','!=',Auth::user()->id)
                                ->first();

    }

    public function render()
    {
            $searchTerm = '%'.$this->searchTerm.'%';
            if($searchTerm){
                $user       = User::where('id','!=',Auth::user()->id)
                            ->where('email','like', $searchTerm)
                                ->get();
            }
            
            $conservation = Message::query()
                ->where('sender_id',  Auth::user()->id)
                ->where('receiver_id',  $this->selectedUser->id)
                ->orWhere('receiver_id',  Auth::user()->id)
                ->where('sender_id', $this->selectedUser->id)
                ->with('sender')
                ->with('receiver')
                ->get();

    return view('livewire.messaging',[

             'users'         => $user,
             'conservation'  =>$conservation
        ]);
    }

    public function viewMessages($userId){

        $this->selectedUser = User::findorFail($userId);

    }

Html 查看

             @if(count($users) > 0)
                @foreach ($users as $user)
                    <a href="#" style="color:#CB6F53"  wire:click.prevent="viewMessages({{ $user->id }} )">
                        <div class="user-card rounded {{ $user->id === $selectedUser->id ? 'bg-dark' : '' }} bg-opacity-10 mb-1  rounded mb-1">
                            <div class="mx-2">
                            <div class="d-flex pt-3">
                                @if($user->avator != Null)
                                <img class="rounded-circle" width="48px" height="48px" src="{{Config('wfh.file').$user->avator}}" alt="">
                                @else
                                <img class="rounded-circle" width="48px" height="48px" src="{{url('')}}/uploads/images/php55DB.tmp.png" alt="">
                                @endif
                                <div class="notification-text ms-3 w-100">
                                <span class="username fw-bold">{{$user->full_name}}</span>
                                <span class="float-end small">{{$user->created_at->format('d-m-Y')}}</span>
                            <p class="mt-1 text-muted">You: {{$user->messages->last()->body}}  </p>
                                <p class="mt-1 text-muted"></p>
                                </div>
                            </div>
                            </div>
                        </div>
                    </a>
                @endforeach
              @else
                <p class="text-center"><b>User not found !</b></p>
              @endif

问题是你的 User-model 上的 messages() 关系没有指定字段,所以它假定相关字段称为 user_id(根据 Laravel 命名约定)。

您可以指定一个字段作为第二个参数,

public function messages()
{
    return $this->hasMany(Message::class, 'sender_id');
}

最后,您应该迫不及待地加载您的消息了,

User::whereNot('id', '!=', Auth::user()->id)
    ->where('email', 'like', $searchTerm)
    ->with('messages') 
    ->get();