代码之家  ›  专栏  ›  技术社区  ›  wenus

具有复合主键的Laravel关系

  •  0
  • wenus  · 技术社区  · 7 年前

    我有3个表,我试图建立order\u products和order\u products\u status\u名称之间的关系。我有一个名为order\u product\u status的转换/透视表。问题是我的主键,因为我在表顺序中有3个主键,我不知道如何通过关系连接这3个表。 我的迁移是:

     public function up()
    {
        Schema::create('order_products', function (Blueprint $table) {
            $table->integer('order_id')->unsigned();
            $table->integer('product_id')->unsigned();
            $table->integer('ordinal')->unsigned();
            $table->integer('size');
    
            $table->primary(['order_id', 'product_id', 'ordinal']);
            $table->foreign('order_id')->references('id')->on('orders');
            $table->foreign('product_id')->references('id')->on('products');
        });
    }
    

    表Order Product status-这是我在Order\u products和Order\u Product\u status\u名称之间的转换/透视表

     public function up()
    {
        Schema::create('order_product_statuses', function (Blueprint $table) {
            $table->integer('order_id')->unsigned();
            $table->integer('product_id')->unsigned();
            $table->integer('status_id')->unsigned();
            $table->integer('ordinal')->unsigned();
            $table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
    
            $table->foreign('order_id')->references('id')->on('orders');
            $table->foreign('status_id')->references('id')->on('order_product_status_names');
            $table->primary(['order_id', 'product_id', 'ordinal']);
        });
    }
    

    public function up()
    {
        Schema::create('order_product_status_names', function (Blueprint $table) {
            $table->integer('id')->unsigned();
            $table->string('name');
            $table->string('code');
            $table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
    
            $table->primary('id');
        });
    }
    

    我知道这里有两种方式的关系blengsToMany,但我不知道或者我可以声明这种关系(从order\u products到order\u product\u status\u names和inverse)?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Zymotik    3 年前

    public function up() {
        Schema::create('order_products', function (Blueprint $table) {
            $table->bigIncrements('id')->unsigned();
            $table->integer('order_id')->unsigned();
            $table->integer('product_id')->unsigned();
            $table->integer('order_product_statuses_id')->unsigned();
            $table->integer('ordinal')->unsigned();
            $table->integer('size');
    
            $table->primary('id');
            $table->foreign('order_id')->references('id')->on('orders');
            $table->foreign('product_id')->references('id')->on('products');
            $table->foreign('order_product_statuses_id')->references('id')->on('order_product_statuses');
        });
    }
    
    public function up() {
        Schema::create('order_product_statuses', function (Blueprint $table) {
            $table->bigIncrements('id')->unsigned();
            $table->integer('product_id')->unsigned();
            $table->integer('status_id')->unsigned();
            $table->integer('ordinal')->unsigned();
            $table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
    
            $table->primary('id');
            $table->foreign('status_id')->references('id')->on('order_product_status_names');
        });
    }
    
    public function up() {
        Schema::create('order_product_status_names', function (Blueprint $table) {
            $table->bigIncrements('id')->unsigned();
            $table->string('name');
            $table->string('code');
            $table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
    
            $table->primary('id');
        });
    }
    

    我希望这能帮你一点忙。