feat: Many category and Many sub-category media
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
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
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('category_media', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('media_id')->constrained('media')->cascadeOnDelete();
|
||||
$table->foreignId('category_id')->constrained('categories')->cascadeOnDelete();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['media_id', 'category_id']);
|
||||
});
|
||||
|
||||
Schema::create('media_sub_category', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('media_id')->constrained('media')->cascadeOnDelete();
|
||||
$table->foreignId('sub_category_id')->constrained('sub_categories')->cascadeOnDelete();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['media_id', 'sub_category_id']);
|
||||
});
|
||||
|
||||
// Backfill the new pivots from the existing single columns.
|
||||
if (Schema::hasColumn('media', 'category_id')) {
|
||||
DB::table('media')
|
||||
->whereNotNull('category_id')
|
||||
->orderBy('id')
|
||||
->select('id', 'category_id')
|
||||
->chunk(200, function ($rows) {
|
||||
$now = now();
|
||||
$insert = $rows->map(fn ($row) => [
|
||||
'media_id' => $row->id,
|
||||
'category_id' => $row->category_id,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
])->all();
|
||||
|
||||
DB::table('category_media')->insertOrIgnore($insert);
|
||||
});
|
||||
}
|
||||
|
||||
if (Schema::hasColumn('media', 'subcategory_id')) {
|
||||
DB::table('media')
|
||||
->whereNotNull('subcategory_id')
|
||||
->orderBy('id')
|
||||
->select('id', 'subcategory_id')
|
||||
->chunk(200, function ($rows) {
|
||||
$now = now();
|
||||
$insert = $rows->map(fn ($row) => [
|
||||
'media_id' => $row->id,
|
||||
'sub_category_id' => $row->subcategory_id,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
])->all();
|
||||
|
||||
DB::table('media_sub_category')->insertOrIgnore($insert);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('media_sub_category');
|
||||
Schema::dropIfExists('category_media');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('media', function (Blueprint $table) {
|
||||
$table->dropForeign(['category_id']);
|
||||
$table->dropColumn('category_id');
|
||||
|
||||
$table->dropForeign(['subcategory_id']);
|
||||
$table->dropColumn('subcategory_id');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('media', function (Blueprint $table) {
|
||||
$table->unsignedBigInteger('category_id')->nullable()->after('image_id');
|
||||
$table->foreign('category_id')->references('id')->on('categories')->nullOnDelete();
|
||||
|
||||
$table->unsignedBigInteger('subcategory_id')->nullable()->after('category_id');
|
||||
$table->foreign('subcategory_id')->references('id')->on('sub_categories')->nullOnDelete();
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user