iT邦幫忙

2021 iThome 鐵人賽

DAY 14
0
永豐金融APIs

掌握訂單與線上金流的剪不斷理還亂系列 第 14

Day14 訂單 -- 多項目結構

  • 分享至 

  • xImage
  •  

昨天看完基礎結構後,有沒有發現紀錄購買商品的部份不是那麼範用,
包含數量、商品項目id、商品單價格等等,全部都紀錄一個欄位,
不只搜尋不方便,統計也很麻煩,
所以我們可以多開一個專門紀錄項目的table方便使用,

//先移除訂單內容description
Schema::create('order', function (Blueprint $table) {
    $table->string('id', 30)->comment('訂單id');
    $table->string('name', 30)->comment('購買人姓名');
    $table->integer('amount')->default(0)->comment('總金額');
    $table->string('status', 20)->default(‘unpaid’)->comment('訂單狀態');
    $table->primary(['id']);
});

//增加item table
Schema::create('order_item', function (Blueprint $table) {
    $table->string('id', 30);
    $table->string('order_id', 30)->comment('訂單id');
    $table->string('name')->comment('商品名稱');
    $table->string('product_id', 30)->comment('商品id');
    $table->longText('product')->comment('商品物件');
    $table->text('photo')->nullable()->comment('商品圖片');
    $table->integer('quantity'); // 數量
    $table->integer('price')->default(0); // 單價
    $table->integer('amount')->default(0); // 總計
    $table->primary(['id']);
    $table->index(['id']);
    $table->index(['order_id']);
});

可以看到我們移除了原本order table的description欄位,
並且增加了order_item table紀錄商品的各項資料,

order_item.id
這邊一樣要有一個流水號,不重複即可,基本上order_item的內容是盡量不去異動的,
包含刪除,因此看要自己產或者用mysql的AUTO_INCREMENT都可以,
但是如果有要人工異動資料的部份,建議自己產生

order_item.order_id
訂單號碼,用於判斷是屬於哪筆訂單的項目。

order_item.name、order_item.product_id
商品名稱跟id這邊做個紀錄,方便filter、group等資料庫操作。

order_item.product
商品物件這邊直接儲存商品購買當下所有資料,包含名稱、圖片、價格、規格等等,
類似於快照的功能,避免發生問題找不到商品購買當下的資料。

order_item.photo
商品圖片這邊是存成陣列,主要是可以做多裝置多size,
也可以用單一圖片或者圖片id做圖片關聯。

其他價錢、數量相關的比較單純,就直接略過不說明
要注意訂單的屬性是要紀錄購買當下的情況,
所以如果只紀錄商品id,然後要資料的時候在拉關聯,
會發生購買當下跟看訂單紀錄金額不一樣的問題,
這是很容易發生在剛入門的錯誤。

這邊只是列出一個基礎框架,如果有想增加的客製化欄位都可以增加,
例如訂單備註、付款時間、建立時間之類的。

接下來會談到簡易的購物車結構,以及常用的購物車功能,
先來去吃烤肉了,明天見


上一篇
Day13 訂單 -- 基礎結構
下一篇
Day15 購物車 -- 基礎結構
系列文
掌握訂單與線上金流的剪不斷理還亂30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言