chat icon indicating copy to clipboard operation
chat copied to clipboard

Extend the models

Open rblijenbergh opened this issue 4 years ago • 4 comments

Is there a way I can extend the models (like Conversation, Participation etc.)?

rblijenbergh avatar Jan 12 '21 19:01 rblijenbergh

me too asking that question

samerzaki avatar Apr 18 '21 12:04 samerzaki

ideal and cleanest would be to have the possibility of providing custom model classes in config - pretty easy to implement, you just have to go through the codebase.

@musonza would you like this functionality? if yes, i might make a PR when i have a bit more time

not ideal, may potentially introduce some problems and will not solve every use case one way to use own models which extend package models is by doing this

Create your own model

<?php

namespace Domain\Chat\Models;

use Illuminate\Database\Eloquent\Relations\HasMany;
use Musonza\Chat\Models\Conversation as ChatConversation;

class Conversation extends ChatConversation
{
    /**
     * Messages in conversation.
     *
     * @return HasMany
     */
    public function messages(): HasMany
    {
        return $this->hasMany(Message::class, 'conversation_id'); //->with('sender');
    }
}

Create a trait which points to your own model implementation

<?php

namespace Domain\Chat\Traits;

use Domain\Chat\Models\Conversation;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

trait InteractsWithChat
{
    /**
     * Chat relation.
     *
     * @return BelongsTo
     */
    public function chat(): BelongsTo
    {
        return $this->belongsTo(Conversation::class, 'conversation_id');
    }
}

this is just a quick thought, i havent thought much about the consequences, but i needed to add one relation of my own to Message model, so i will see how it goes for now

theimerj avatar Apr 27 '21 17:04 theimerj

Yep, extending package models - core functionality on my opinion. But solution with traits - dirty. Maybe extending of base Chat:class will be is more flexible

Forsakenrox avatar May 12 '21 15:05 Forsakenrox

Best way is to extend the core model and add your own functions like @theimerj suggested.

tommie24 avatar May 13 '21 13:05 tommie24