插入的行key
,value
爲空!Laravel - 在hasmany上創建不工作
\App\Product::find(1)->getProductMeta()->create([
'key' => 'a key',
'value' => 'a value'
]);
查詢SQL: insert into `product_metas` (`product_id`) values (?)
產品:
function getProductMeta(){
return $this->hasMany('App\ProductMeta');
}
ProductMeta:
protected $fillable = ['key','value'];
遷移 :
Schema::create('product_metas', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned()->index();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');;
$table->string('key')->index();
$table->text('value')->nullable();
});
分享這兩個模型的表結構,以及如何獲取'$ product'? – Sandeesh
@Sandeesh它在這裏,在更新 –
如果你尋求幫助,你需要發佈適當的代碼。你沒有公佈我問過的一半?只需複製粘貼您的遷移和控制器方法即可。 – Sandeesh