<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\DB;

class PermissionTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $routeCollection = Route::getRoutes();

        foreach ($routeCollection as $value) {
            if($value->getName() != "" && strpos($value->getName(), 'generated::') === false){
                $existPermission = Permission::where("name",$value->getName())->first();
                if(empty($existPermission)){
                    Permission::insertOrIgnore(array('name' => $value->getName(), 'guard_name'=>"web"));
                }
            }
        }
        $role = Role::where(['name' => 'Super Admin'])->first();
     
        $permissions = Permission::pluck('id','id')->all();
   
        $role->syncPermissions($permissions);
    }
}
