<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class DepartmentSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Set the connection to the external database
        DB::connection('old_db');

        // Fetch records from the external database using Eloquent
        $externalRecords = \App\Models\OldDB\Department::all();

        DB::connection('mysql');
        $this->reset();

        // Insert records into your local database
        foreach ($externalRecords as $record) {
            \App\Models\Department::insert([
                'id' => $record->id,
                'name' => $record->dept_name,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ]);


        }
    }

    public function reset() {
        \App\Models\Department::truncate();
    }
}
