belongsTo(Course::class); } public function type() { return $this->belongsTo(CourseType::class, 'course_type_id'); } public function duration() { return $this->belongsTo(CourseDuration::class, 'course_duration_id'); } public function frequency() { return $this->belongsTo(CourseFrequency::class, 'course_frequency_id'); } public function level() { return $this->belongsTo(CourseLevel::class, 'course_level_id'); } public function category() { return $this->belongsTo(Category::class, 'category_id'); } public function member() { return $this->belongsTo(Member::class); } public function getins() { return $this->belongsTo(User::class, 'instructor_id'); } public function childs() { return $this->hasMany(\App\Models\Course::class,'parent_id','id') ; } public function getTree() { $str = ''; if ($this->parent_id != null) { $a = $this->recursiveName($this->parent_id, array($this->name)); $a = array_reverse($a); $str = implode(" - ", $a); } else { $str = $this->name; } return $str; } public function recursiveName($parent_id, $array) { $x = \App\Models\Course::findOrFail($parent_id); $array[] = $x->name; if ($x->parent_id != null) { return $this->recursiveName($x->parent_id, $array); } else { return $array; } } public function recursiveParent($parent_id, $array) { if ($parent_id == null) return $array; $x = \App\Models\Course::findOrFail($parent_id); $array[] = $x->id; if ($x->parent_id != null) { return $this->recursiveParent($x->parent_id, $array); } else { return $array; } } public function getCount() { return \App\Models\MemberCourse::where('course_id', $this->id)->count(); } public function getDetailsName() { $courseName = $this->name ?? 'Corso Sconosciuto'; $levelName = is_object($this->level) ? $this->level->name : ''; $frequencyName = is_object($this->frequency) ? $this->frequency->name : ''; $displayNameParts = [$courseName]; if ($levelName) $displayNameParts[] = $levelName; if ($frequencyName) $displayNameParts[] = $frequencyName; return implode(' - ', $displayNameParts); } }