1

Im trying to make a relationship in laravel between 3 tables. First we have the "decks" table, which contains the information of every deck with id of the owner. Then we have the "cards" table, which only contains the information for the cards. Finally, I have two pivot tables, one named CardsAndUsers which contains an id, card_id and user_id, and another called CardsInDecks which only contains cards_users_id and deck_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 column cardindecks.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 method attach() of a many-to-many relationship. I just had to pass carduser_id and modify my cards_in_decks table and add this field.

Example:

$deck = Deck::find("5d09525cad67b");
$deck->cards()->attach(1, ['carduser_id' => 3]);

I feel very silly.

0 Answers0