| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- class Course extends Model
- {
- use HasFactory;
- protected $fillable = [
- 'parent_id',
- 'name',
- 'course_type_id',
- 'causal_id',
- 'max_members',
- 'instructor',
- 'price',
- 'months',
- 'date_from',
- 'date_to',
- 'course_duration_id',
- 'course_frequency_id',
- 'course_level_id',
- 'enabled',
- 'year',
- 'subscription_price',
- 'sub_causal_id',
- 'category_id'
- ];
- public function parent()
- {
- return $this->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 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();
- }
- }
|