php - How to fix: Relationships between two pivot tables
Im trying to make a relationship in laravel between 3 tables. First we have the "decks
" table, which contains the information of every deck withid
of the owner. Then we have the "cards
" table, which only contains the information for the cards. Finally, I have two pivot tables, one namedCardsAndUsers
which contains anid, card_id
anduser_id
, and another calledCardsInDecks
which only containscards_users_id
anddeck_id
.
The problem I have is that i dont know how to relation all of this.
I'm working with laravel 5.8
This is the structure of the tables
cards
id
title
description
cost
type
kingdom
image
health
attack
decks
id
title
description
user_id
cards_and_users
id
card_id
user_id
cards_in_decks
card_and_user_id
user_id
Deck migration
Schema::create('decks', function (Blueprint $table) {
$table->string('id', 255)->unique();
$table->string('title', 255);
$table->text('description');
$table->string('user_id', 255);
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
Card Migration
Schema::create('cards', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title', 255);;
$table->text('description');
$table->unsignedInteger('cost');
$table->string('type', 255);
$table->string('kingdom', 255);
$table->string('image', 255);
$table->integer('health');
$table->integer('attack');
$table->timestamps();
});
Cards_and_users Migration
Schema::create('cards_and_users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('user_id', 255);
$table->unsignedBigInteger('card_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('card_id')->references('id')->on('cards')->onDelete('cascade');
$table->timestamps();
});
cards_in_decks Migration
Schema::create('cards_in_decks', function (Blueprint $table) {
$table->unsignedBigInteger('carduser_id');
$table->string('deck_id', 255);
$table->foreign('carduser_id')->references('id')->on('cards_and_users')->onDelete('cascade');
$table->foreign('deck_id')->references('id')->on('decks')->onDelete('cascade');
$table->timestamps();
});
Card.php
{
protected $fillable = [
'title', 'description', 'cost', 'type', 'kingdom', 'image', 'health', 'attack'
];
public function users(){
return $this->belongsToMany(User::class, 'cards_and_users')>withPivot('card_id', 'user_id')->withTimestamps();
}
public function decks(){
return $this->belongsToMany(Deck::class, 'cards_in_decks')->withPivot('carduser_id', 'deck_id')->withTimestamps();
}
}
Deck.php
class Deck extends Model
{
protected $primaryKey = 'id';
public $incrementing = false;
protected $keyType = 'string';
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'string',
];
protected $fillable = [
'id', 'title', 'description', 'user_id',
];
public function cards(){
return $this->belongsToMany(Card::class, 'cards_in_decks')->withPivot('carduser_id', 'deck_id')->withTimestamps();
}
public function user(){
return $this->belongsTo(User::class);
}
}
I get an error saying that the columncardindecks.card_id
doesnt exist (obviously). But i don't know why laravel expects it to exist.
EDIT 1
After researching I discovered i can pass more than one paremeter with the methodattach()
of a many-to-many relationship. I just had to passcarduser_id
and modify mycards_in_decks
table and add this field.
Example:
$deck = Deck::find("5d09525cad67b");
$deck->cards()->attach(1, ['carduser_id' => 3]);
I feel very silly.