Make migration and model for batu permata feature

This commit is contained in:
amirhassan
2024-01-31 08:48:18 +08:00
parent f056561979
commit b239106ba8
2 changed files with 79 additions and 1 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ class Marhun extends Model
* @var array
*/
protected $fillable = [
'kodcaw','marhun','t_gadai','norujukan','bil','nota','karat','berat','harga','n_marhun'
'kodcaw','marhun','t_gadai','norujukan','bil','nota','karat','berat','harga','n_marhun','batu_permata','berat_batu_permata',
];
/**
@@ -0,0 +1,78 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddBatuPermataToMarhun extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
/**
*
* @var object $db_connection
*/
public function getActiveDB()
{
$config_db = array_keys(config("database.connections"));
$db_connection = array_filter($config_db, function ($connection) {
try {
if ($connection == 'mysql7' || $connection == 'lelong')
return false;
$db = DB::connection($connection)->getPdo();
return true;
} catch (\Exception $e) {
return false;
}
});
return $db_connection;
}
public function up()
{
$db_connection = $this->getActiveDB();
foreach ($db_connection as $dbase) {
Schema::connection($dbase)->table('marhun', function (Blueprint $table) use ($dbase) {
$isBatuPermataExist = Schema::connection($dbase)->hasColumn('marhun', 'batu_permata');
$isBatuPermataExist = Schema::connection($dbase)->hasColumn('marhun', 'berat_batu_permata');
if (!$isBatuPermataExist) {
$table->string('batu_permata')->nullable();
$table->decimal('berat_batu_permata', 7, 2)->nullable();
}
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$db_connection = $this->getActiveDB();
foreach ($db_connection as $dbase) {
$columns = ['batu_permata', 'berat_batu_permata'];
$columns_exist = array_filter($columns, function ($column) use ($dbase) {
return Schema::connection($dbase)->hasColumn('marhun', $column);
});
Schema::connection($dbase)->table('marhun', function (Blueprint $table) use ($columns_exist) {
foreach ($columns_exist as $column) {
$table->dropColumn($column);
}
});
}
}
}