Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the models #275

Open
rblijenbergh opened this issue Jan 12, 2021 · 4 comments
Open

Extend the models #275

rblijenbergh opened this issue Jan 12, 2021 · 4 comments

Comments

@rblijenbergh
Copy link

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

@samerzaki
Copy link

me too asking that question

@theimerj
Copy link

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

@Forsakenrox
Copy link

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

@tommie24
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants