feat: add rating and comement

This commit is contained in:
2026-05-20 12:19:10 +03:30
parent e446de79fd
commit 6f5224a5d9
11 changed files with 547 additions and 32 deletions
@@ -0,0 +1,33 @@
<?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('comments', function (Blueprint $table) {
$table->dropForeign(['media_id']);
$table->dropColumn('media_id');
// Add polymorphic columns
$table->morphs('commentable');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('comments', function (Blueprint $table) {
$table->dropMorphs('commentable');
$table->foreignId('media_id')->constrained()->onDelete('cascade');
});
}
};
@@ -0,0 +1,65 @@
<?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::dropIfExists('ratings');
Schema::create('ratings', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')
->constrained()
->cascadeOnDelete();
// polymorphic relation
$table->morphs('rateable');
$table->tinyInteger('stars'); // 1-5
$table->timestamps();
// one rating per user per item
$table->unique([
'user_id',
'rateable_id',
'rateable_type'
], 'ratings_user_rateable_unique');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('ratings');
// optional: rollback old structure (if you still need it)
Schema::create('ratings', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')
->constrained()
->cascadeOnDelete();
$table->foreignId('media_id')
->constrained()
->cascadeOnDelete();
$table->tinyInteger('stars');
$table->timestamps();
$table->unique(['user_id', 'media_id']);
});
}
};