5.7. 收藏商品
收藏商品
收藏商品是电商网站一个常用的功能,本章节要实现收藏商品的基本功能。
1. 数据库结构
收藏商品本质上是用户和商品的多对多关联,因此不需要创建新的模型,只需要增加一个中间表即可:
$ php artisan make:migration create_user_favorite_products_table --create=user_favorite_products
注意:中间表命名,要越直白越好,名字长点也无所谓。一个简单的判断命名是否合格的方法是 —— 想象自己半年一年以后是否能快速地从数据库表名得知此表的功能。
database/migrations/< your_date >_create_user_favorite_products_table.php
.
.
.
public function up()
{
Schema::create('user_favorite_products', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->...