2

I am trying to build some chat system using Laravel and i have these 2 models: User and Thread

The User model has Messagable Trait where you can get all the threads with

$user->threads();

I am trying to eager load additional data to the threads array using the following:

$threads = Auth::user()->threads()->with(['participants.user'])->get();

What i am struggling is the Threads model has function to get the latest message from it:

$thread->getLatestMessage();

My question is how can i append this latest message to the upper query i am doing. I was trying something like this but its not ok... I guess im doing something stupid here...

$threads = Auth::user()->threads()->with([
        'participants.user',
        'latestMessage' => function ($query) {
            return $query->getLatestMessageAttribute();
        }])->get();

or

$threads = Auth::user()->threads()->with(['participants.user','getLatestMessageAttribute'])->get();

I hope i clarified this ok because i am using a 3rd party package for this system which has these Traits and Thread classes i am using.

SOLUTION

Looks like i had to add append('accessor_name') at the end when getting the collection.

$collection = Auth::user()->relationship()->get()->append('accessor_name');
Nenad Kaevik
  • 177
  • 1
  • 4
  • 19

1 Answers1

1

You can override class .Create new model and extend package model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;


class Thread extends \Lexx\ChatMessenger\Models\Thread
{
    use HasFactory;

    protected $appends=['latest_message'];
    

}

Publish config:

php artisan vendor:publish --provider="Lexx\ChatMessenger\ChatMessengerServiceProvider" --tag="config"

in config/chatmessenger.php

'thread_model' => \App\Models\Thread::class,

Updated If anyone still not getting get attribute data then dont forget to clear cache

php artisan cache:clear

php artisan optimize

php artisan config:clear

php artisan clear
John Lobo
  • 14,355
  • 2
  • 10
  • 20
  • I did this and now when i try doing the following... The latest_message is not inside the object Auth::user()->threads()->with(['participants.user'])->get(); – Nenad Kaevik Jun 25 '21 at 11:36
  • 1
    it might not in object but try accessing that object.Also try public function getLatestMessageAttribute() { return $this->messages()->latest()->first(); } this method in new model – John Lobo Jun 25 '21 at 11:38
  • 1
    @NenadKaevik.if you are using laravel 8 then update 'user_model' => App\Models\User::class, – John Lobo Jun 25 '21 at 11:52
  • Did all of this and i still cannot see the latest_message column in the response object – Nenad Kaevik Jun 25 '21 at 11:59
  • instead of dd($thread) .try once accessing dd($thread->latest_message) – John Lobo Jun 25 '21 at 12:01
  • @NenadKaevik if that doesn't work then create your own accessor new model and try whether it will append to objecy – John Lobo Jun 25 '21 at 12:02
  • 1
    last option i can create new method public function latestMessage() { return $this->messages()->latest()->first(); } and access like $thread->latestMessage(); – John Lobo Jun 25 '21 at 12:05
  • For gods sake man this pissed me off... This is how i solved it ... $threads = Auth::user()->threads()->with('participants.user','messages')->get()->append('latest_message'); I had to call append('accessor') after getting the collection ... – Nenad Kaevik Jun 25 '21 at 12:25
  • oops but i really didnt know why it doesn't work .i dont have pusher account or else i would have given one try – John Lobo Jun 25 '21 at 12:26
  • 1
    That was weird though ... It was supposed to work like you mentioned above... I got no idea tbh... Anyways man ... appreciate your help :D Finally got this figured out ... Thanks a TON! – Nenad Kaevik Jun 25 '21 at 12:28
  • @NenadKaevik.add your sollution in answer and accept your answer as accepted.so it help someone to solve. – John Lobo Jun 25 '21 at 12:40
  • @NenadKaevik. i have tried using appends it works well https://ibb.co/ydjNNPz ....try clearing cache or run php artisan optimize – John Lobo Jun 25 '21 at 14:41
  • Omg... true ... Stupid cache -_- – Nenad Kaevik Jun 25 '21 at 15:37