表的结构如下:
REATE TABLE `carts` (
`user_id` int(10) unsigned NOT NULL,
`product_id` char(64) NOT NULL,
`quantity` int(10) unsigned NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`user_id`,`product_id`),
KEY `fk_idx_cart_product` (`product_id`),
CONSTRAINT `fk_idx_cart_product` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`),
CONSTRAINT `fk_idx_cart_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
需要用Eloquent ORM的save()方法,他会根据primaryKey来保存数据,请问如何在Model设置这种复合主键?
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'id';
$cart = Cart::firstOrNew([...]);
$cart->quantity = $quantity;
$cart->save();
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause'
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
人生最曼妙的风景,竟是内心的淡定与从容!