iT邦幫忙

2025 iThome 鐵人賽

DAY 14
0
Modern Web

Laravel 是甚麼系列 第 14

增加購物車裡面數量

  • 分享至 

  • xImage
  •  

購物車增加數量欄位
用指令: php artisan make:migration add_quantity_to_cart_items
https://ithelp.ithome.com.tw/upload/images/20250815/20119035npPgIRg048.png
修改程式碼:更新跟復原都要
https://ithelp.ithome.com.tw/upload/images/20250815/20119035QicYsPBKzU.png
程式碼

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddQuantityToCartItems extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('cart_items', function (Blueprint $table) {
            $table ->integer('quantity') ->after('cart_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('cart_items', function (Blueprint $table) {
            $table ->dropColumn('quantity')
        });
    }
}

更新沒顯示先手動加入

https://ithelp.ithome.com.tw/upload/images/20250815/20119035tWiD31XKqh.png

terminal執行: php artisan migrate
https://ithelp.ithome.com.tw/upload/images/20250815/20119035yH2vQDgNSD.png
加入 CartItemController
用指令php artisan make:controller CartItemController --resource

https://ithelp.ithome.com.tw/upload/images/20250815/20119035DOPPvRwg26.png

加入 路由Route::resource('cart_items', 'CartItemController');
https://ithelp.ithome.com.tw/upload/images/20250815/20119035pyAG2LJmpk.png

打開加入
use Illuminate\Support\Facades\DB;
https://ithelp.ithome.com.tw/upload/images/20250815/201190354CDCv7AcwR.png
修改程式碼
https://ithelp.ithome.com.tw/upload/images/20250815/20119035Zh9BIgkL0N.png
程式碼內容

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CartItemController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $form = $request->all();
        DB::table('cart_items')->insert(['cart_id' => $form['cart_id'],
'product_id' => $form['product_id'],
'quantity' => $form['quantity'],
'created_at' => now(),
'updated_at' => now()]);

return response(true);
    }

在terminal要啟動php artisan serve
https://ithelp.ithome.com.tw/upload/images/20250815/20119035MccqGWOpiW.png

用POSTMAN的POST測試localhost:8000/cart_items
在BODY選x-www-..

https://ithelp.ithome.com.tw/upload/images/20250815/20119035mnI5yTYXuo.png

到資料表看有增加
https://ithelp.ithome.com.tw/upload/images/20250815/20119035iLS8RFJviI.png

把顯示1改成顯示true
改程式碼return response()->json(true);
https://ithelp.ithome.com.tw/upload/images/20250815/20119035z92kZMKxsk.png
程式碼:

 public function store(Request $request)
    {
        $form = $request->all();
        DB::table('cart_items')->insert(['cart_id' => $form['cart_id'],
'product_id' => $form['product_id'],
'quantity' => $form['quantity'],
'created_at' => now(),
'updated_at' => now()]);

//return response(true);
return response()->json(true);
    }
    
    ```



用POSTMAN的POST顯示
 ![https://ithelp.ithome.com.tw/upload/images/20250815/20119035bubnfP71PP.png](https://ithelp.ithome.com.tw/upload/images/20250815/20119035bubnfP71PP.png)
更新的程式碼:只有更新數量/時間
 ![https://ithelp.ithome.com.tw/upload/images/20250815/20119035nu0B4PdB4o.png](https://ithelp.ithome.com.tw/upload/images/20250815/20119035nu0B4PdB4o.png)
 
 
 

public function update(Request $request, $id)
{
$form = $request->all();
DB::table('cart_items')->where('id',$id)
->update([
'quantity' => $form['quantity'],
'updated_at' => now()]);

//return response(true);
return response()->json(true);
}



POSTMAN測試localhost:8000/cart_items/1?quantity=5
 ![https://ithelp.ithome.com.tw/upload/images/20250815/20119035L0sYuIXTun.png](https://ithelp.ithome.com.tw/upload/images/20250815/20119035L0sYuIXTun.png)
資料表查看
 
![https://ithelp.ithome.com.tw/upload/images/20250815/201190352qLrAcK9Hk.png](https://ithelp.ithome.com.tw/upload/images/20250815/201190352qLrAcK9Hk.png)
刪除 程式碼:
 ![https://ithelp.ithome.com.tw/upload/images/20250815/20119035JqyzcOON8m.png](https://ithelp.ithome.com.tw/upload/images/20250815/20119035JqyzcOON8m.png)
 
 
 

public function destroy($id)
{

    DB::table('cart_items')->where('id',$id)
    ->delete();

return response()->json(true);
}
}


![https://ithelp.ithome.com.tw/upload/images/20250815/201190351Qd4fem2lV.png](https://ithelp.ithome.com.tw/upload/images/20250815/201190351Qd4fem2lV.png)
資料表看
![https://ithelp.ithome.com.tw/upload/images/20250815/20119035l9KY4NOFYA.png](https://ithelp.ithome.com.tw/upload/images/20250815/20119035l9KY4NOFYA.png)
大家明天見~
沒有顯示圖片的部分再麻煩大家貼上網址查看~

上一篇
開始建購物車
下一篇
加入條件判斷
系列文
Laravel 是甚麼30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言