Causal.php 1.6 KB

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