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

时间:2019-06-17 12:25:25

标签: php laravel many-to-many relationship

我正在尝试在laravel中建立3个表之间的关系。首先,我们有一个“ decks”表,其中包含拥有所有者id的每个牌组的信息。然后,我们有“ cards”表,其中仅包含卡的信息。最后,我有两个数据透视表,一个名为CardsAndUsers,其中包含一个id, card_iduser_id,另一个名为CardsInDecks,其中仅包含cards_users_id和{{1 }}。

我的问题是我不知道如何关联所有这些。

我正在使用laravel 5.8

这是表格的结构

deck_id

甲板迁移

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

卡迁移

        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();
        });


Cards_and_users迁移

        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_in_decks迁移

        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();
        });

Card.php

        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();
        });

Deck.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();
  }
}

我收到一条错误消息,指出(显然)列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); } } 不存在。但是我不知道为什么拉拉维尔希望它存在。

编辑1

研究之后,我发现我可以使用多对多关系的方法cardindecks.card_id通过多个参数。我只需要通过attach()并修改我的carduser_id表并添加此字段即可。

示例:

cards_in_decks

我觉得很傻。

0 个答案:

没有答案