我有一个Product
对象,其中有多个Shop
对象,因为商店可以以不同的价格/条件提供相同的产品。
我对产品有一个编辑视图,其中列出了可以购买该产品的商店。
当我调整产品商店时,例如价钱;我收到商店中已经存在数据库的错误。我知道产品存在,但是我需要更新数据。
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-1' for key 'PRIMARY'
public function update(Request $request,$slug)
{
$product = Product::with('shops','type')->where('slug',$slug)->first();
[... snip ...]
$i = 0;
foreach($product->shops as $shop) {
$shop = request('shop');
$product->shops()->attach($product->id,[
'shop_id' => $shop[$i]['id'],'price' => $shop[$i]['price'],'url' => $shop[$i]['url']
]);
$i++;
}
$product->save();
return redirect('/'.$slug)->with('success','Product has been updated');
}
$product->update();
产生相同的结果。
编辑:
Product.php
class Product extends Model
{
protected $appends = ['lowest_price'];
public function shops(){
return $this->belongsToMany('App\Shop')->withPivot('price','url');
}
public function type(){
return $this->belongsTo('App\Type');
}
public function getLowestPriceAttribute()
{
$lowest_price = NULL;
foreach($this->shops as $shop) {
if(is_null($lowest_price)) {
$lowest_price = (double)$shop->pivot->price;
}
if($lowest_price > (double)$shop->pivot->price) {
$lowest_price = (double)$shop->pivot->price;
}
}
return $lowest_price;
}
}
Shop.php
class Shop extends Model
{
//
}
商店迁移
public function up()
{
Schema::create('shops',function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('url');
$table->string('logo');
$table->timestamps();
});
[... snip ...]
}
EDIT2:
有关该错误的更多信息:
Illuminate \ Database \ QueryException (23000)
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-1' for key 'PRIMARY' (SQL: insert into `product_shop` (`price`,`product_id`,`shop_id`,`url`) values (500.00,1,http://test.com))
'CREATE TABLE `products` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`make` varchar(255) COLLATE utf8_unicode_ci NOT NULL,`model` varchar(255) COLLATE utf8_unicode_ci NOT NULL,`description` text COLLATE utf8_unicode_ci NOT NULL,`image` varchar(255) COLLATE utf8_unicode_ci NOT NULL,`video` varchar(255) COLLATE utf8_unicode_ci NOT NULL,`manufacturer_specs` text COLLATE utf8_unicode_ci NOT NULL,`top_speed` decimal(8,1) NOT NULL,`range` decimal(8,`weight` decimal(8,`type_id` int(10) unsigned NOT NULL,`slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,`lowest_price` decimal(8,`created_at` timestamp NULL DEFAULT NULL,`updated_at` timestamp NULL DEFAULT NULL,PRIMARY KEY (`id`),UNIQUE KEY `products_slug_unique` (`slug`),KEY `products_type_id_index` (`type_id`),CONSTRAINT `products_type_id_foreign` FOREIGN KEY (`type_id`) REFERENCES `types` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
'CREATE TABLE `product_shop` (
`product_id` int(10) unsigned NOT NULL,`shop_id` int(10) unsigned NOT NULL,`price` decimal(8,2) NOT NULL,`url` text COLLATE utf8_unicode_ci NOT NULL,PRIMARY KEY (`product_id`,`shop_id`),KEY `product_shop_product_id_index` (`product_id`),KEY `product_shop_shop_id_index` (`shop_id`),CONSTRAINT `product_shop_product_id_foreign` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE,CONSTRAINT `product_shop_shop_id_foreign` FOREIGN KEY (`shop_id`) REFERENCES `shops` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
'CREATE TABLE `shops` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,`url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,`logo` varchar(255) COLLATE utf8_unicode_ci NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'