syntaxlexx / laravel-5-messenger

A Simple Laravel 5, 6, 7, 8, 9, 10 & 11 Messenger with Pusher Capabilities
https://messenger-inertia.acelords.com/
MIT License
104 stars 32 forks source link

Extend Messages Model #16

Closed ahmadmukhtarpl closed 4 years ago

ahmadmukhtarpl commented 4 years ago

I'm using your package for chat in LARAVEL 6. Now I want to extend the functionality of this package by allowing the upload of attachments, one message can have multiple attachments. To achieve this I need to override your Message Model so I can define the relationships to the attachment model. How can I do that without editing the vendor file?

syntaxlexx commented 4 years ago

Hi @ahmadmukhtarpl ,

Considering your situation, it might be unsuitable to extend your custom 'Message' model with the package's Message model. Same case goes to traits. One option I can see working is creating a custom function in your 'helpers.php' class, say getMessageAttachments() and call is like so in your blade or controller:

$attachments = getMessageAttachments($message);
function getMessageAttachments(Message $message) 
{
     $attachments = Attachment::where('message_id', $message->id);

     // you can add filters here e.g. check if a user is allowed to view the file and such.
    // So you need to update your function signature accordingly

   return $attachments->get();
}

Let me know how that goes.