【问题标题】:Laravel One-to-Many relation - InsertionLaravel 一对多关系 - 插入
【发布时间】:2015-03-16 15:50:43
【问题描述】:

我有 3 个表:Order、Contract、Order_contract。

  • 一份合约(可以)有(因为多个订单最终价格可以 结合在一起)多个订单
  • 但是一个订单可以有一个 合同

我创建了另一个表 (order_contract) 来连接订单和合同。 迁移如下:

public function up()
{
    Schema::create('contracts', function(Blueprint $table)
    {
        $table->increments('id');
        $table->timestamps();
    });

    Schema::create('orders', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('price');
        $table->timestamps();
    });

    Schema::create('order_contract', function(Blueprint $table)
    {
        $table->integer('order_id')->unsigned();
        $table->foreign('order_id')->references('id')->on('orders');
        $table->integer('contract_id')->unsigned();
        $table->foreign('contract_id')->references('id')->on('contracts');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('contracts');
    Schema::drop('orders');
    Schema::drop('order_contract');
}

我想将数据插入到我的表中。 1. 插入新合同(插入时我知道$contract->id) 2. 如果一个订单附加了多个订单,则将每个关系插入到 order_contract 表中

型号:

**Order.php**

class Order extends Eloquent{
protected $fillable = ['price'];

public function contract(){
    return $this->belongsTo('Contract');
}

}

**Contract.php**

class Contract extends Eloquent{

public function orders(){
    return $this->hasMany('Order','order_contract','order_id','contract_id');
}

}

在这种情况下如何使用 Laravel 的 hasOne(),hasMany(),belongsTo(),belongsToMany() 函数?

【问题讨论】:

    标签: join laravel-4 relationship belongs-to


    【解决方案1】:

    当您使用 OneToMany 关系时,您正在创建一个中间表。只有在创建多对多关系时才需要这样做。

    删除 order_contact 表,并在您的 orders 表上添加一列“contract_id”(您可以选择将其设为空,因此订单不必有合同)。

    然后你可以给你的Contract模型添加一个函数

    class Contract extends Eloquent {
    
        public function orders()
        {
            return $this->hasMany('Order');
        }
    }
    

    和你的Order 模型

    class Order extends Eloquent {
    
        public function contract()
        {
            return $this->belongsTo('Contract');
        }
    }
    

    然后你可以这样做:

    $order1 = new Order;
    $order2 = new Order;
    $contract = new Contract;
    $contract->orders()->saveMany([$order1, $order2]);
    

    查看Attaching A Related Model 上的文档


    如果你坚持通过中间表来做,你可以这样做:

    class Contract extends Eloquent {
    
        public function orders()
        {
            return $this->hasManyThrough('Order', 'Contract', 'order_id', 'contract_id');
        }
    }
    

    请注意,Eloquent 假设您有一个中间模型。

    Laravel 中没有 BelongsToManyThrough 函数,所以你必须编写自己的方法。 hasManyThrough 只是一个快捷方式,并不打算以这种方式使用...

    我仍然建议不要这样做。

    【讨论】:

    • 我认为为关系制作另一个表是一个聪明的解决方案,不要用其他东西填充订单表,并使每个表尽可能简单。我同意,您的解决方案是正确的,并且可以接受。但是您知道如何使用“中间”表吗?谢谢
    • 我理解您为什么要这样做,但我强烈建议您考虑另一种方式。我已经更新了我的答案。
    • 最后我选择了没有中间表的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    相关资源
    最近更新 更多