Causal.php 1.6 KB

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