<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class CompaniesUsersSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $skipAdministrator = 0;
        $skipAdministrative = 0;
        $skipAssistant = 0;
        $skipEmployee = 0;
        foreach (\App\Models\Company::all() as $company) {
            $company->users()->attach(\App\Models\User::role('Empleado')->skip($skipEmployee)->take(5)->get());
            $company->users()->attach(\App\Models\User::role('Administrador')->skip($skipAdministrator)->take(1)->get());
            $company->users()->attach(\App\Models\User::role('Recursos humanos')->skip($skipAdministrative)->take(1)->get());
            $company->users()->attach(\App\Models\User::role('Asistente Administrativo')->skip($skipAssistant)->take(1)->get());
            $skipAdministrator++;
            $skipAdministrative++;
            $skipAssistant++;
            $skipEmployee += 5;
        }
        $company_id = DB::table('users')
        ->join('company_user', 'users.id', '=', 'company_user.user_id')
        ->join('companies', 'company_user.company_id', '=', 'companies.id')
        ->select('companies.id as company_id')
        ->where('users.id', 2)
        ->get()->first();
        $company=\App\Models\Company::find($company_id->company_id);
        $company->users()->detach(\App\Models\User::find(2));
        $company->users()->detach(\App\Models\User::find(4));
        $company->users()->detach(\App\Models\User::find(5));
        $company_3mas1r=\App\Models\Company::find(1);
        $company_3mas1r->users()->attach(\App\Models\User::find(2));
        $company_3mas1r->users()->attach(\App\Models\User::find(4));
        $company_3mas1r->users()->attach(\App\Models\User::find(5));
    }
}
