1.先創建migration

php artisan make:migration create_carts_and_cart_items

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a9382df5-a56f-4a50-a495-10192f31c33e/1627747359996.jpg

2.建立好相關欄位名稱,這邊將carts及cart_items分開建立table

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6e436fc6-fecb-40da-bc9e-3b8fc214e7f2/1627747982301.jpg

3.執行指令創建table

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/801e8b5e-908f-46d4-9511-baff42e27230/1627748233199.jpg

4.創建后我們先準備一些假資料到product,並到postman看看資料是否都有近來

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ceec9001-4acf-4796-bf8e-ec19f5cd29f2/1627749916787.jpg

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/1619f087-06df-4add-a23a-c702f7974488/1627750006557.jpg

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/64af0a48-6cc6-43ec-938e-e32ba0228586/1627749992283.jpg

5.接下來製作cart controller,這邊使用resource幫我們產生index store 等function

php artisan make:controller CartController --resource

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/2539f75f-7dad-4aef-8c09-7314b8c8d603/1627809542287.jpg

6.接著設定路由

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/5f08681d-9f21-4e8f-8cf5-ac2de2f71165/1627809721564.jpg

7.引入DB套件

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/880e3b71-ed72-4777-acd3-33a0476ee885/1627813313805.jpg

8.再取到資料response的時候是一個物件的型態,故我們要轉成array或是collect的型態

public function index()
    {
        $cart = DB::table('carts')->get()->first();
        return response(collect($cart));
    }