chat
chat copied to clipboard
Extend the models
Is there a way I can extend the models (like Conversation, Participation etc.)?
me too asking that question
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
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
Best way is to extend the core model and add your own functions like @theimerj suggested.