<?php

namespace App\Models;

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

class PermissionsUserModel extends Model
{
    use HasFactory;
    protected $table = "permissions_users";
    static public function InsertUpdateRecord($permission_ids, $user_id){
        PermissionsUserModel::where("user_id", $user_id)->delete();
        foreach ($permission_ids as $permission_id){
            $save = new PermissionsUserModel();
            $save->permissions_id = $permission_id;
            $save->user_id = $user_id;
            $save->save();
        }
    }
    static public function getUserPermissions($user_id) {
        return PermissionsUserModel::where("user_id", $user_id)->get();
    }
    static public function getPermission($slug, $user_id) {
     
        return PermissionsUserModel::select('permissions_users.id')
        ->join('permissions_1', 'permissions_1.id', '=', 'permissions_users.permissions_id')
        ->where('permissions_users.user_id', '=', $user_id)
        ->where('permissions_1.slug', '=', $slug)
        ->count();
    }
    
}