Update Koperasi
This commit is contained in:
+43
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Koperasi\PengurusKoperasi;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('pengurus_koperasi', function (Blueprint $table) {
|
||||
$table->foreignId('desa_kelurahan_id')->nullable()->constrained('master_desa_kelurahan')->nullOnDelete();
|
||||
$table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
|
||||
});
|
||||
|
||||
if (! Schema::hasTable('activity_log')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Penginput lama hanya dipulihkan dari log pembuatan, bukan dari pengguna yang terakhir mengubah.
|
||||
DB::table('pengurus_koperasi')->select('id')->whereNull('created_by')->chunkById(100, function ($rows) {
|
||||
foreach ($rows as $row) {
|
||||
$creator = DB::table('activity_log')->where('subject_type', (new PengurusKoperasi)->getMorphClass())
|
||||
->where('subject_id', $row->id)->where('event', 'created')
|
||||
->where('causer_type', (new User)->getMorphClass())->orderBy('id')->value('causer_id');
|
||||
if ($creator && DB::table('users')->where('id', $creator)->exists()) {
|
||||
DB::table('pengurus_koperasi')->where('id', $row->id)->update(['created_by' => $creator]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('pengurus_koperasi', function (Blueprint $table) {
|
||||
$table->dropConstrainedForeignId('created_by');
|
||||
$table->dropConstrainedForeignId('desa_kelurahan_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user