The structure of the table is as follows:
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
Need to use eloquent ORM's Save () method, he will save the data according to PrimaryKey, how to set this compound primary key in 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 '
Reply content:
The structure of the table is as follows:
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
Need to use eloquent ORM's Save () method, he will save the data according to PrimaryKey, how to set this compound primary key in 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 '
This problem is not resolved, most of the other framework is not resolved, so there is no way to define a composite primary key in the eloquent ORM, the solution:
1. Use doctrine.
2. Validation in form validation can also achieve the effect of primary key constraints.
On this issue in fact, a year ago, a foreign programmer on GitHub issue, and Taylorotwell also participated in the discussion, he gave the reply is laravel currently no plan to increase this feature. This feature was not present until today (2015.11.07). Please see the following link for details: https://github.com/laravel/framework/issues/5355