<?php

namespace App\Models;

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

class PermissionRolesModel extends Model
{
    use HasFactory;
    protected $table = "permissions_rules";
    static public function InsertUpdateRecord($permission_ids, $role_id){
        PermissionRolesModel::where("role_id", $role_id)->delete();
        foreach ($permission_ids as $permission_id){
            $save = new PermissionRolesModel();
            $save->permissions_id = $permission_id;
            $save->role_id = $role_id;
            $save->save();
        }
    }
    static public function getRolePermissions($role_id) {
        return PermissionRolesModel::where("role_id", $role_id)->get();
    }
    static public function getPermission($slug, $role_id) {

        return PermissionRolesModel::select('permissions_rules.id')
        ->join('permissions_1', 'permissions_1.id', '=', 'permissions_rules.permissions_id')
        ->where('permissions_rules.role_id', '=', $role_id)
        ->where('permissions_1.slug', '=', $slug)
        ->count();
    }

}
