Add Deck + pivot tables

main
Alex 2 years ago
parent b18d98ae81
commit 8c7ef7c187

@ -8,4 +8,24 @@ use Illuminate\Database\Eloquent\Model;
class Deck extends Model
{
use HasFactory;
/**
* The tags that belong to the Deck
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags(): BelongsToMany
{
return $this->belongsToMany(Tag::class, 'decks_tags', 'tags_id', 'decks_id');
}
/**
* Get all of the Cards for the Deck
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function cards(): HasMany
{
return $this->hasMany(Card::class, 'deck_id', 'card_id');
}
}

@ -14,11 +14,11 @@ return new class extends Migration
Schema::create('decks', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description')->nullable();
$table->integer('card_count');
$table->string('cover_path');
$table->string('backside_path');
$table->string('type')->nullable();
$table->timestamps();
});
}

@ -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::create('cards_tags', function (Blueprint $table) {
$table->id();
$table->unsignedBiginteger('cards_id')->unsigned();
$table->unsignedBiginteger('tags_id')->unsigned();
$table->foreign('cards_id')->references('id')->on('cards')->onDelete('cascade');
$table->foreign('tags_id')->references('id')->on('tags')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('cards_tags');
}
};
Loading…
Cancel
Save