Skip to content

Instantly share code, notes, and snippets.

@ragasubekti
Created August 6, 2018 16:03
Show Gist options
  • Save ragasubekti/ea5204c7c715a627b3f5f5efe51fb0dd to your computer and use it in GitHub Desktop.
Save ragasubekti/ea5204c7c715a627b3f5f5efe51fb0dd to your computer and use it in GitHub Desktop.
<?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