Laravel雄辩的非常规专栏不起作用

时间:2022-10-25 09:43:35

Hi laravel is not inserting correct value in pivot table for many to many case.

对于很多情况,嗨laravel没有在数据透视表中插入正确的值。

Here my first model is

这是我的第一个模型

class ShippingAddress extends Eloquent {
  protected $guarded = array('id');
  protected $table = 'shippingAddress';

  public function mwsOrder()
  {
    return $this->belongsToMany('MwsOrder',
      'mwsOrders_shippingAddress',
      'Address_id',
      'AmazonOrderId'
    );
  }
}

Second Model is

第二个模型是

class MwsOrder extends Eloquent {
  protected $table = 'mwsOrders';
  protected $primaryKey = 'AmazonOrderId';

  public function shippAddress()
  {
    return $this->belongsToMany('ShippingAddress',
        'mwsOrders_shippingAddress',
        'AmazonOrderId',
        'Address_id'
      );
   }

}

EER Diagram Laravel雄辩的非常规专栏不起作用

Now when i run this

现在,当我运行这个

$mwsOrder = new MwsOrder;
$mwsOrder->AmazonOrderId = 'Eve 6';
$mwsOrder->save();

$address = new ShippingAddress;
$address->name = 'Naruto Uzumaki';
$address->save();
$address->mwsOrder()->attach($mwsOrder);
//$mwsOrder->shippAddress()->save($address);

laravel throws error and this is what laravel trying to run the query

laravel抛出错误,这就是laravel尝试运行查询的原因

(SQL: insert into mwsOrders_shippingAddress (Address_id, AmazonOrderId) values (1, 3))

(SQL:插入mwsOrders_shippingAddress(Address_id,AmazonOrderId)值(1,3))

What i need is to generate this query

我需要的是生成此查询

insert into mwsOrders_shippingAddress (Address_id, AmazonOrderId) values (1, 'Eve 6')

插入mwsOrders_shippingAddress(Address_id,AmazonOrderId)值(1,'Eve 6')

Update: Schema are:

更新:架构是:

Schema::create("shippingAddress", function(Blueprint $table)
{
  $table->increments("id");
  $table->string("Name");
  $table->timestamps();
});

Schema::create("mwsOrders", function(Blueprint $table)
{
  $table->increments("id");
  $table->string("AmazonOrderId")->unique();
  $table->timestamps();
});

Schema::create("mwsOrders_shippingAddress", function(Blueprint $table)
{
  $table->increments("id");
  $table->string("AmazonOrderId");
  $table->foreign("AmazonOrderId")->references("AmazonOrderId")->on('mwsOrders');
  $table->integer("shipping_address_id")->unsigned();
  $table->foreign("shipping_address_id")->references('id')->on('shippingAddress');
  $table->timestamps();
});

1 个解决方案

#1


1  

At first change the shippAddress to this:

首先将shippAddress更改为:

// Specify the primary key because it's not conventional id
protected $primaryKey = 'AmazonOrderId';

public function shippAddress()
{
    return $this->belongsToMany('ShippingAddress',
        'mwsOrders_shippingAddress',
        'AmazonOrderId',
        'Address_id'
    );
}

Then you may try this:

然后你可以尝试这个:

$mwsOrder = new MwsOrder;
$mwsOrder->AmazonOrderId = 'Eve 6';
$mwsOrder->save();

$address = new ShippingAddress(['name' => 'Naruto Uzumaki']);
$mwsOrder->shippAddress()->save($address); // Save and Attach

#1


1  

At first change the shippAddress to this:

首先将shippAddress更改为:

// Specify the primary key because it's not conventional id
protected $primaryKey = 'AmazonOrderId';

public function shippAddress()
{
    return $this->belongsToMany('ShippingAddress',
        'mwsOrders_shippingAddress',
        'AmazonOrderId',
        'Address_id'
    );
}

Then you may try this:

然后你可以尝试这个:

$mwsOrder = new MwsOrder;
$mwsOrder->AmazonOrderId = 'Eve 6';
$mwsOrder->save();

$address = new ShippingAddress(['name' => 'Naruto Uzumaki']);
$mwsOrder->shippAddress()->save($address); // Save and Attach