Skip to content

Instantly share code, notes, and snippets.

@kura1420
Created June 28, 2025 02:45
Show Gist options
  • Select an option

  • Save kura1420/8b9f4f527a726c940b1ff7e9ca69fa1c to your computer and use it in GitHub Desktop.

Select an option

Save kura1420/8b9f4f527a726c940b1ff7e9ca69fa1c to your computer and use it in GitHub Desktop.
Laravel migrate data from same table but outher connection
<?php
namespace App\Console\Commands;
use App\Models\ActivityLog;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
class MigrateDataCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'migrate:data';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
//
try {
$chunk = 1000;
$query = DB::table('activity_log');
$total = $query->count();
$this->info("Total Data: {$total}");
$this->output->progressStart($total);
$query->orderBy('id')->chunk($chunk, function (Collection $rows) use ($chunk) {
foreach ($rows->toArray() as $key => $row) {
$t = new ActivityLog();
$t->log_name = $row->log_name;
$t->description = $row->description;
$t->subject_type = $row->subject_type;
$t->subject_id = $row->subject_id;
$t->causer_type = $row->causer_type;
$t->causer_id = $row->causer_id;
$t->properties = $row->properties;
$t->created_at = $row->created_at;
$t->updated_at = $row->updated_at;
$t->batch_uuid = $row->batch_uuid;
$t->event = $row->event;
$t->timestamps = false;
$t->save();
}
$this->output->progressAdvance($chunk);
});
$this->output->progressFinish();
$this->info('Done');
} catch (\Throwable $th) {
throw $th;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment