dongqiangse6623 2019-06-17 12:25
浏览 65

如何修复:两个数据透视表之间的关系

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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测