zubair syslab
zubair syslab

Reputation: 169

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

I am building a simple private chat app with laravel and livewire.

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

I am trying to show the last sender message to receiver but unfortunately i am getting error https://flareapp.io/share/B5Z18q07#F83 please help me how can i resolved that ? thank u.

enter image description here enter image description here

User Model

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 view

             @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

Upvotes: 0

Views: 478

Answers (1)

Qirel
Qirel

Reputation: 26450

The problem is that the messages() relation on your User-model has not specified a field, so it assumes that the related field is called user_id (as per Laravel naming conventions).

You could specify a field as the second argument,

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

Finally, you should be eager loading your messages,

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

Upvotes: 1

Related Questions