Causal.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. 'enabled'
  14. ];
  15. public function childs() {
  16. return $this->hasMany(\App\Models\Causal::class,'parent_id','id') ;
  17. }
  18. public function getTree()
  19. {
  20. $str = '';
  21. if ($this->parent_id != null)
  22. {
  23. $a = $this->recursiveName($this->parent_id, array($this->name));
  24. $a = array_reverse($a);
  25. $str = implode(" - ", $a);
  26. }
  27. else
  28. {
  29. $str = $this->name;
  30. }
  31. return $str;
  32. }
  33. public function recursiveName($parent_id, $array)
  34. {
  35. $x = \App\Models\Causal::findOrFail($parent_id);
  36. $array[] = $x->name;
  37. if ($x->parent_id != null)
  38. {
  39. return $this->recursiveName($x->parent_id, $array);
  40. }
  41. else
  42. {
  43. return $array;
  44. }
  45. }
  46. }