laravel RBAC implement the second method

class RbacController extends Controller
{
        // rbac second method
    public function show()
    {
        // query the data
    	$data = DB::SELECT("select * from rbac");

        // call the method
    	$arr = $this->rbac($data);
    	var_dump ($ arr);
    }
    

    public function rbac($data,$pid = 0,$level = 0)
    {
        // define an array
    	static $arr = [];

        // Loop through
    	foreach ($data as $key => $v) {
    		if ($v->pid == $pid) {
    			$v->level = $v->name;
    			$arr[] = $v;
    			unset($data[$key]);

                // call the method again
    			$this->rbac($data,$v->id,$level+1);
    		}
    	}
    	return $arr;
    }

  

Guess you like

Origin www.cnblogs.com/funbaby/p/11094007.html