From 8c7ef7c1876592763da7708e92c0a379be296439 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 8 Jul 2023 22:19:37 +0200 Subject: [PATCH] Add Deck + pivot tables --- src/app/Models/Deck.php | 20 +++++++++++ .../2023_07_08_142515_create_decks_table.php | 2 +- ...3_07_08_173842_create_cards_tags_table.php | 33 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/database/migrations/2023_07_08_173842_create_cards_tags_table.php diff --git a/src/app/Models/Deck.php b/src/app/Models/Deck.php index f9ef161..f42dab4 100644 --- a/src/app/Models/Deck.php +++ b/src/app/Models/Deck.php @@ -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'); + } } diff --git a/src/database/migrations/2023_07_08_142515_create_decks_table.php b/src/database/migrations/2023_07_08_142515_create_decks_table.php index 8163743..c88888f 100644 --- a/src/database/migrations/2023_07_08_142515_create_decks_table.php +++ b/src/database/migrations/2023_07_08_142515_create_decks_table.php @@ -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(); }); } diff --git a/src/database/migrations/2023_07_08_173842_create_cards_tags_table.php b/src/database/migrations/2023_07_08_173842_create_cards_tags_table.php new file mode 100644 index 0000000..e52b5cf --- /dev/null +++ b/src/database/migrations/2023_07_08_173842_create_cards_tags_table.php @@ -0,0 +1,33 @@ +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'); + } +};