Created
August 6, 2018 16:03
-
-
Save ragasubekti/ea5204c7c715a627b3f5f5efe51fb0dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Model; | |
use Illuminate\Database\Eloquent\Collection; | |
use Illuminate\Database\Eloquent\Model; | |
class Opd extends Model | |
{ | |
protected $table = 'master_kantor'; | |
public function child() | |
{ | |
return $this->hasMany('App\Model\Opd', 'kdparent', 'kdlokasi'); | |
} | |
public function parent() | |
{ | |
return $this->belongsTo('App\Model\Opd', 'kdparent', 'kdlokasi'); | |
} | |
public function childRecursive() | |
{ | |
return $this->child()->with('childRecursive'); | |
} | |
public function getChildrensWithParent() | |
{ | |
return $this->getChildrens($this->childRecursive); | |
} | |
public function getChildrens($obj) | |
{ | |
$obj = json_decode(json_encode($obj)); | |
$child = new Collection; | |
if (gettype($obj) == 'array') { | |
foreach ($obj as $item) { | |
$arr = $this->getChildrens($item); | |
foreach ($arr as $itm) { | |
$child->push($itm); | |
} | |
} | |
} else { | |
if (count($child) <= 0) { | |
$child->push($obj->kdparent); | |
} | |
$child->push($obj->kdlokasi); | |
if ($obj->child_recursive && count($obj->child_recursive) > 0) { | |
$rec = $this->getChildrens($obj->child_recursive); | |
foreach ($rec as $item) { | |
$child->push($item); | |
} | |
} | |
} | |
return $child; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment