Causal.php 1.6 KB

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