php – Laravel雄辩的非传统专栏无法正常工作

php – Laravel雄辩的非传统专栏无法正常工作,第1张

概述对于很多情况,嗨laravel没有在数据透视表中插入正确的值. 我的第一个模型是 class ShippingAddress extends Eloquent { protected $guarded = array('id'); protected $table = 'shippingAddress'; public function mwsOrder() { retu 对于很多情况,嗨laravel没有在数据透视表中插入正确的值.

我的第一个模型是

class ShipPingAddress extends Eloquent {  protected $guarded = array('ID');  protected $table = 'shipPingAddress';  public function mwsOrder()  {    return $this->belongsToMany('MwsOrder','mwsOrders_shipPingAddress','Address_ID','AmazonorderID'    );  }}

第二种模式是

class MwsOrder extends Eloquent {  protected $table = 'mwsOrders';  protected $primaryKey = 'AmazonorderID';  public function shippAddress()  {    return $this->belongsToMany('ShipPingAddress','AmazonorderID','Address_ID'      );   }}

EER图

现在,当我运行这个

$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抛出错误,这就是laravel尝试运行查询的原因

(sql: insert into mwsOrders_shipPingAddress (Address_ID,
AmazonorderID) values (1,3))

我需要的是生成此查询

insert into mwsOrders_shipPingAddress (Address_ID,‘Eve 6’)

更新:
架构是:

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();});
解决方法 首先将shippAddress更改为:

// Specify the primary key because it's not conventional IDprotected $primaryKey = 'AmazonorderID';public function shippAddress(){    return $this->belongsToMany('ShipPingAddress','Address_ID'    );}

然后你可以试试这个:

$mwsOrder = new MwsOrder;$mwsOrder->AmazonorderID = 'Eve 6';$mwsOrder->save();$address = new ShipPingAddress(['name' => 'Naruto Uzumaki']);$mwsOrder->shippAddress()->save($address); // Save and Attach
总结

以上是内存溢出为你收集整理的php – Laravel雄辩的非传统专栏无法正常工作全部内容,希望文章能够帮你解决php – Laravel雄辩的非传统专栏无法正常工作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1251118.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存