I have a Message::class for which I want to get thread messages for each particular message.
how can I recursively fetch messages and messages of messages etc. etc. using Laravel and Eloquent::hasMany
So far I tried setting up a recursive on the hasMany()
public function replys()
{
return $this->hasMany(Message::class, 'reply_to', 'id');
}
public function replies()
{
$r = $this->replys;
if(count($r->get('replys')) > 0){
foreach($r->get('replys') as $reply) {
$r->push(Message::create($reply)->replies());
}
}
return $r;
}
But I seem to be missing the mark. At the moment I'm trying to see if I can use a pivot table and reference belongsToMany
. Will let yo know how things progress