| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?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',
- 'enabled'
- ];
- public function parent()
- {
- return $this->belongsTo(Course::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;
- }
- }
- }
|