<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PermissionsModel extends Model
{
    use HasFactory;
    protected $table = "permissions_1";

    static public function getRecords(){

        $getPermissions =  PermissionsModel::groupBy("groupby")->get();
        $result = array();

        foreach($getPermissions as $value){
            $getPermissionsGroup = PermissionsModel::getPermissionsGroup($value->groupby);
            $data = array();
            $data["id"] =  $value->id;
            $data["name"] = $value->name;
            $data["groupby"] = $value->groupby;
            $data["module_name"] = $value->module_name;

            $group = array();
            foreach($getPermissionsGroup as $valueG){
                $dataG = array();
                $dataG["id"] = $valueG->id;
                $dataG["name"] = $valueG->name;
                $dataG["groupby"] = $value->groupby;
                $dataG["module_name"] = $value->module_name;

                $group[] = $dataG;

            }
            $data['group'] = $group;   
            $result[] = $data;

    }
    return $result; 
}
    static public function getPermissionsGroup($groupby){
        
        return PermissionsModel::where("groupby", '=',$groupby)->get();
    }
    static public function getRecord($id){
        
        return RoleModel::find($id);
    }
}
