dongzhouhao4316 2018-05-16 19:19
浏览 38

Laravel - 在数据透视表中创建其他列是一个好习惯吗?

So, i have a project on which the client can order food. The products are stored in a cart and then are inserted in a database. I want to design the Orders and Products tables but i also want to store the quantity and the subtotal price for each product.Orders and Products have N:N relation. Here are my table migrations:

Schema::create('orders', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->integer('product_id');
            $table->enum('status',['open','closed']);
            $table->timestamps();
        });

Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('description')->nullable();
            $table->float('price',8,2);
            $table->string('image_path');
            $table->timestamps();
        });

The pivot table:

Schema::create('order_product', function (Blueprint $table) {
            $table->integer('order_id')->unsigned()->nullable();
            $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
            $table->integer('product_id')->unsigned()->nullable();
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->timestamps();
        });

The ways I see the solution are:

  1. add the quantity and subtotal attributes to the pivot table( which i am not sure if it is good or bad)
  2. insert rows for each of the specific product quantity (which will create a lot of rows for the same order and i'm not sure if this is good too).

Can you point me to a better solution? I am new to web development.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题