ctrlpanel/database/migrations/2022_06_16_092704_add_billing_period_to_products.php

44 lines
1 KiB
PHP

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class AddBillingPeriodToProducts extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('products', function (Blueprint $table) {
$table->string('billing_period')->default("hourly");
});
DB::statement('UPDATE products SET billing_period="hourly"');
$products = DB::table('products')->get();
foreach ($products as $product) {
$price = $product->price;
$price = $price / 30 / 24;
DB::table('products')->where('id', $product->id)->update(['price' => $price]);
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('billing_period');
});
}
}