Causal.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. 'money',
  13. 'user_status',
  14. 'no_first',
  15. 'no_records',
  16. 'enabled'
  17. ];
  18. public function childs() {
  19. return $this->hasMany(\App\Models\Causal::class,'parent_id','id') ;
  20. }
  21. public function getTree()
  22. {
  23. $str = '';
  24. if ($this->parent_id != null)
  25. {
  26. $a = $this->recursiveName($this->parent_id, array($this->name));
  27. $a = array_reverse($a);
  28. $str = implode(" - ", $a);
  29. }
  30. else
  31. {
  32. $str = $this->name;
  33. }
  34. return $str;
  35. }
  36. public function recursiveName($parent_id, $array)
  37. {
  38. $x = \App\Models\Causal::findOrFail($parent_id);
  39. $array[] = $x->name;
  40. if ($x->parent_id != null)
  41. {
  42. return $this->recursiveName($x->parent_id, $array);
  43. }
  44. else
  45. {
  46. return $array;
  47. }
  48. }
  49. public function recursiveParent($parent_id, $array)
  50. {
  51. if ($parent_id == null)
  52. return $array;
  53. $x = \App\Models\Causal::findOrFail($parent_id);
  54. $array[] = $x->id;
  55. if ($x->parent_id != null)
  56. {
  57. return $this->recursiveParent($x->parent_id, $array);
  58. }
  59. else
  60. {
  61. return $array;
  62. }
  63. }
  64. }