我的第一个模型是
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雄辩的非传统专栏无法正常工作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)