Hi!
I want to create Message model which has from, to, text and conversation fields.
Fields from and to can be a Admin or User or Member model.
Filed conversation can be null which means it's just a single message, or can be a Conversation model which means it's a message of a conversation.
Filed text is just a text!
I'm not sure how to implement these! Do I need to use Polymorphic Relations?
Yes, you need MorphTo relationships for from and to and two columns each.
Schema::create('messages', function (Blueprint $table) {
[...]
$table->morphs('from');
$table->morphs('to');
[...]
});
class Message extends Model {
public function from() {
return $this->morphTo();
}
public function to() {
return $this->morphTo();
}
}
public function messagesFromMe()
{
return $this->morphMany(Message::class, 'from');
}
public function messagesToMe()
{
return $this->morphMany(Message::class, 'to');
}