Causal.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Causal extends Model
  6. {
  7. use HasFactory;
  8. protected $fillable = [
  9. 'parent_id',
  10. 'name',
  11. 'type',
  12. 'enabled'
  13. ];
  14. public function childs() {
  15. return $this->hasMany(\App\Models\Causal::class,'parent_id','id') ;
  16. }
  17. public function getTree()
  18. {
  19. $str = '';
  20. if ($this->parent_id != null)
  21. {
  22. $a = $this->recursiveName($this->parent_id, array($this->name));
  23. $a = array_reverse($a);
  24. $str = implode(" - ", $a);
  25. }
  26. else
  27. {
  28. $str = $this->name;
  29. }
  30. return $str;
  31. }
  32. public function recursiveName($parent_id, $array)
  33. {
  34. $x = \App\Models\Causal::findOrFail($parent_id);
  35. $array[] = $x->name;
  36. if ($x->parent_id != null)
  37. {
  38. return $this->recursiveName($x->parent_id, $array);
  39. }
  40. else
  41. {
  42. return $array;
  43. }
  44. }
  45. }