Home > Back-end >  I am trying to get the name of the message sender using laravel 8
I am trying to get the name of the message sender using laravel 8

Time:05-25

I am building a simple private chat app with laravel.

I have a messages table with a column : id/sender_id/receiver_id and body

Please tell me how can i get the name of message sender ?. i tried following Please give me solution.

Note :- Actually message receiver is not receiving name of message sender with message. when sender message sent the message to receiver so receiver is getting the name of message receiver instead name of message sender.

enter image description here

enter image description here

Controller

               $conservation = Message::where(function ($q) {
                        $q->where('sender_id', Auth::user()->id)
                        ->orWhere('receiver_id', Auth::user()->id);
                })->where(function ($q) {
                    $q->where('sender_id', $this->selectedUser->id)
                      ->orWhere('receiver_id', $this->selectedUser->id);
                })->get();

Message Model

 public function sender()
    {
        return $this->belongsTo(User::class,'sender_id');
    }

    public function receiver()
    {
        return $this->belongsTo(User::class,'receiver_id');
    }

User Table

  public function receiver(){

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

    public function sender()
    {
        return $this->hasMany(Message::class);
    }

html view

             @foreach ($conservation as $message)
              <div >
                <div ><span > 
                 </span>{{$message->created_at->format('d-m-Y') }}</div>
                <div >
                     @if ($message->sender_id === Auth::user()->id)
                     <b><p>{{$message->receiver->full_name}}</p></b>
                    @else
                    <b><p>{{$message->sender->full_name}}</p></b>
                    @endif
                    <div >
                    <div>{{$message->body}}</div>
                  </div>
                </div>
              </div>
              @endforeach

CodePudding user response:

Something like this should work just fine.

Message::query()
->where('receiver_id', $user_id)
->with('sender')
->get();

If you want to optimize the query, you can have this

Message::query()
->where('receiver_id', $user_id)
->with('sender:id,first_name,last_name')
->get();

I assumed that you've first_name, last_name columns. If you don't have them, you can replace them with your name column/columns.

In my opinion, you should also paginate the messages for performance reasons. Getting all the messages like this can be pretty costly since there might be too many messages.

CodePudding user response:

Edited: First, you need to send receiver_id from client-side to your backend ($receiver_id). Then this code will consider two conditions: 1) you are the receiver and looking at the sender's message page. 2) you are the sender and looking at the receiver's message page.

return Message::query()
        ->where('sender_id',  Auth::id())
        ->where('receiver_id',  $receiver_id)
        ->orWhere('receiver_id',  Auth::id())
        ->where('sender_id', $receiver_id)
        ->with('sender')
        ->with('receiver')
        ->get();

Then you need to add some If conditions based on the authenticated user id in your client code.

  • Related