iT邦幫忙

2025 iThome 鐵人賽

DAY 20
0
Modern Web

Laravel 是甚麼系列 第 20

測試CRUD含登入

  • 分享至 

  • xImage
  •  

註冊新的帳號
https://ithelp.ithome.com.tw/upload/images/20250827/20119035Xn0SaJuyCC.png
登入
https://ithelp.ithome.com.tw/upload/images/20250827/20119035VXxiYtYUee.png
複製token放到
再按一次 send 顯示不同
https://ithelp.ithome.com.tw/upload/images/20250827/20119035nxcjBDMCpQ.png
購物車加上資料
加入token
https://ithelp.ithome.com.tw/upload/images/20250827/20119035hq74twY0cE.png

回到
https://ithelp.ithome.com.tw/upload/images/20250827/20119035QKwjCUqAQN.png
登出

https://ithelp.ithome.com.tw/upload/images/20250827/201190354NhoXDyQoV.png
https://ithelp.ithome.com.tw/upload/images/20250827/20119035ByLieIXf9B.png


用terminal增加php artisan make:migration create_orders_and_order_items
https://ithelp.ithome.com.tw/upload/images/20250829/20119035L8TBAeCYdL.png
修改程式碼:
https://ithelp.ithome.com.tw/upload/images/20250829/20119035Z2ah3CSLf2.png
往下
https://ithelp.ithome.com.tw/upload/images/20250829/20119035YeRRyvyoD8.png
terminal執行php artisan migrate
Models資料夾裡面新增檔案
https://ithelp.ithome.com.tw/upload/images/20250829/20119035f1V6JE6HkK.png
複製其他檔案貼上修改
https://ithelp.ithome.com.tw/upload/images/20250829/20119035hDG4rTHeaU.png
再新增檔案
https://ithelp.ithome.com.tw/upload/images/20250829/20119035diUzeIojWG.png
修改Product程式碼
https://ithelp.ithome.com.tw/upload/images/20250829/201190353Z8ewUUojz.png
修改Cart 程式碼
https://ithelp.ithome.com.tw/upload/images/20250829/20119035y0koLnqonR.png
在terminal下指令檢查 php artisan tinker
https://ithelp.ithome.com.tw/upload/images/20250829/2011903514RfF327fi.png
做結帳下指令php artisan make:migration add_checked_to_carts
https://ithelp.ithome.com.tw/upload/images/20250829/2011903507dFlnspZo.png
修改程式碼
https://ithelp.ithome.com.tw/upload/images/20250829/20119035oP4aMxm34z.png
下指令跑起來: php artisan migrate
修改 Cart程式碼
https://ithelp.ithome.com.tw/upload/images/20250829/20119035STtyfiZm4k.png
下指令測試php artisan tinker
再修改 Cart程式碼
https://ithelp.ithome.com.tw/upload/images/20250829/20119035VNemtUJ3u5.png
修改 CartController 程式碼:
https://ithelp.ithome.com.tw/upload/images/20250829/20119035WX6dxzwhYt.png
修改 User程式碼:
https://ithelp.ithome.com.tw/upload/images/20250829/20119035wjrspSH36Z.png
修改 CartController 程式碼:
https://ithelp.ithome.com.tw/upload/images/20250829/20119035YMNbMvEifg.png
Terminal下指令測試php artisan tinker
修改路由:
https://ithelp.ithome.com.tw/upload/images/20250829/20119035zVAvFUYTjG.png
如果有,但 orders 表仍然不存在,說明遷移未執行,你可以強制重新遷移:
php artisan migrate:refresh
資料表看
https://ithelp.ithome.com.tw/upload/images/20250829/20119035Ktvu6XzpUU.png
https://ithelp.ithome.com.tw/upload/images/20250829/20119035OPa94bOf96.png

https://ithelp.ithome.com.tw/upload/images/20250829/20119035xvocCgiw3i.png
有到localhost:8000/carts/checkout
資料表就會註記從0變1
https://ithelp.ithome.com.tw/upload/images/20250829/20119035Os6n5jQLrE.png
結帳只能到這裡(已提問)
https://ithelp.ithome.com.tw/upload/images/20250829/20119035ldcZRLgbDM.png
加入$order->orderItems;
就有了
顯示
https://ithelp.ithome.com.tw/upload/images/20250829/20119035XwxLK9qNxF.png

修改CartController程式碼只顯示未結帳的
https://ithelp.ithome.com.tw/upload/images/20250829/20119035OBJQ6M5Lbb.png
加入VIP價格
https://ithelp.ithome.com.tw/upload/images/20250829/20119035lUumZZ2k4j.png
在terminal打 執行php artisan migrate
修改Cart 程式碼
https://ithelp.ithome.com.tw/upload/images/20250829/20119035g13pCPPrPv.png
下面加入判斷
https://ithelp.ithome.com.tw/upload/images/20250829/20119035CoKlUqiKGT.png
再改price的部分
https://ithelp.ithome.com.tw/upload/images/20250829/20119035yvRkmL5tzl.png
價格20
https://ithelp.ithome.com.tw/upload/images/20250829/20119035iThh9rbiwf.png
Level2的結帳是16
https://ithelp.ithome.com.tw/upload/images/20250829/20119035wkeN7OmjDi.png
顯示
https://ithelp.ithome.com.tw/upload/images/20250829/20119035Jdm0IO3Ow3.png
做數量的檢查
引入套件:
修改CartItemController 程式碼
https://ithelp.ithome.com.tw/upload/images/20250829/20119035YrO8CSfYRO.png
修改 Product程式碼
https://ithelp.ithome.com.tw/upload/images/20250829/20119035lAnOZfxK6E.png
修改CartItemController程式碼
https://ithelp.ithome.com.tw/upload/images/20250829/20119035juCHs6JJLv.png

看資料表products數量
https://ithelp.ithome.com.tw/upload/images/20250829/20119035El2Ab3Pd6E.png
POSTMAN測試
https://ithelp.ithome.com.tw/upload/images/20250829/20119035NZNBXK0eGg.png
放在購物車很久才結帳
修改Cart 程式碼
修改前程式碼:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Cart extends Model
{
    use HasFactory;

protected $guarded = [''];
private $rate = 1;
    public function cartItems()
    {
        return $this->hasMany(CartItem::class);
    }   
    public function user()
    {
        return $this->belongsTo(User::class);
    }
    public function order()
    {
        return $this->hasOne(Order::class);
    }
    public function checkout()
    {
        $order = $this->order()->create([
'user_id' => $this->user_id,
]);
if($this->user->level == 2){
    $this->rate = 0.8;
}

foreach ($this->cartItems as $cartItem) {
    $order->orderItems()->create([
        'product_id' => $cartItem->product_id,
        'price' => $cartItem->product->price * $this->rate
    ]);

}
$this->update(['checkouted' => true]);
$order->orderItems;
return $order;

    }
}

https://ithelp.ithome.com.tw/upload/images/20250829/20119035FhYbTAgQQv.png

修改後Cart程式碼:

https://ithelp.ithome.com.tw/upload/images/20250829/20119035kYfBScmOzS.png

修改Product 程式碼 加上protected $guarded =[''];表示欄位都可以被更新
https://ithelp.ithome.com.tw/upload/images/20250829/20119035dV1WvLPCvZ.png

有結帳資料庫的數量會變少:修改後Cart程式碼:

https://ithelp.ithome.com.tw/upload/images/20250829/20119035Gqv7i6pJQP.png
https://ithelp.ithome.com.tw/upload/images/20250829/20119035ojux8mffrA.png
POSTMAN測試
https://ithelp.ithome.com.tw/upload/images/20250829/20119035VpEWE2NlfO.png
顯示

https://ithelp.ithome.com.tw/upload/images/20250829/20119035p6cMQQhQ3x.png
資料庫數量真的有變少
https://ithelp.ithome.com.tw/upload/images/20250829/20119035DePAuCxYjG.png

https://ithelp.ithome.com.tw/upload/images/20250829/20119035d5ml6aq9Dk.png

https://ithelp.ithome.com.tw/upload/images/20250829/20119035j0aPDfLGkS.png

https://ithelp.ithome.com.tw/upload/images/20250829/20119035bh7RLdfRnf.png

https://ithelp.ithome.com.tw/upload/images/20250829/20119035TeE1NPmtxA.png


https://ithelp.ithome.com.tw/upload/images/20250829/20119035h3b9tAhUn4.png
https://ithelp.ithome.com.tw/upload/images/20250829/20119035kNF6vqVTOA.png
大家明天見~


上一篇
要有token才可以
下一篇
開始前端
系列文
Laravel 是甚麼30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言