<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class Rtm extends Model
{
	use HasFactory;

	protected $table = 'rtm';
	public $timestamps = FALSE;

	/* RELATIONS */

	/**
	 * Subordinate RTM entries
	 *
	 * @return \Illuminate\Database\Eloquent\Relations\HasMany
	 */
	public function children()
	{
		return $this->hasMany(self::class,'parent_id');
	}

	/**
	 * Return all the children RTM records that this record is parent of
	 *
	 * @return Collection
	 */
	public function children_all(): Collection
	{
		$result = collect();
		$result->push($this->withoutRelations());

		foreach ($this->children as $o)
			$result = $result->merge($o->children_all());

		return $result;
	}
}