iT邦幫忙

2025 iThome 鐵人賽

DAY 13
0
Modern Web

Laravel 是甚麼系列 第 13

開始建購物車

  • 分享至 

  • xImage
  •  

變成carts跟cart_items兩個表格
一對多的程式
https://ithelp.ithome.com.tw/upload/images/20250814/20119035Fkgk4xRnjx.png
復原也要修改

<?php

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

class CreateCartsAndCartItems extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('carts', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
        Schema::create('cart_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('cart_id');
            $table->foreignId('product_id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('carts');
        Schema::dropIfExists('cart_items');
    }
    
}

Terminal下指令去更新資料庫php artisan migrate
https://ithelp.ithome.com.tw/upload/images/20250814/201190355iT1fD4JZR.png

改成連$data = DB::table('products')->get();
https://ithelp.ithome.com.tw/upload/images/20250814/20119035OwoxDBhME4.png

在資料表內有2個測試資料
https://ithelp.ithome.com.tw/upload/images/20250814/20119035gYfIIARc0f.png

在terminal執行php artisan serve
https://ithelp.ithome.com.tw/upload/images/20250814/20119035qDaLeWOa3Q.png

用POSTMAN可以看到localhost:8000/products
https://ithelp.ithome.com.tw/upload/images/20250814/20119035OfWGbnj6Tn.png

要新增 購物車的controller在terminal下指令
php artisan make:controller CarController --resource
https://ithelp.ithome.com.tw/upload/images/20250814/20119035ZAEvWIiNJI.png

自動產生的程式碼:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CarController 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)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

修改內容:
https://ithelp.ithome.com.tw/upload/images/20250814/20119035UUvGm1tBqh.png

use Illuminate\Support\Facades\DB;

 public function index()
    {
        $cart = DB::table('carts')->get()->first();
        if(empty($cart)){
            DB::table('carts')->insert(['created_at' =>now(),'updated_at' =>now()]);
            $cart = DB::table('carts')->get()->first();
        }

        return response(collect($cart));
    }
    
    ```

要加入 路由: Route::resource('carts', 'CartController'); 

 ![https://ithelp.ithome.com.tw/upload/images/20250814/20119035H9TCyIpbJF.png](https://ithelp.ithome.com.tw/upload/images/20250814/20119035H9TCyIpbJF.png)

再到terminal讓他跑起來php artisan serve

![https://ithelp.ithome.com.tw/upload/images/20250814/20119035iwqerDmE63.png](https://ithelp.ithome.com.tw/upload/images/20250814/20119035iwqerDmE63.png)
 
資料表內有建的資料
![https://ithelp.ithome.com.tw/upload/images/20250814/201190351AlHDDAwnb.png](https://ithelp.ithome.com.tw/upload/images/20250814/201190351AlHDDAwnb.png)
 
用POSTMAN查看localhost:8000/carts
![https://ithelp.ithome.com.tw/upload/images/20250814/201190355P0nv7cUV4.png](https://ithelp.ithome.com.tw/upload/images/20250814/201190355P0nv7cUV4.png)

![https://ithelp.ithome.com.tw/upload/images/20250814/201190351RUmG9zFPz.png](https://ithelp.ithome.com.tw/upload/images/20250814/201190351RUmG9zFPz.png)
 
cart_items裡面有一些資料
![https://ithelp.ithome.com.tw/upload/images/20250814/201190354ObFHEoRRt.png](https://ithelp.ithome.com.tw/upload/images/20250814/201190354ObFHEoRRt.png)
 
修改 程式碼帶入cart_items資料表
 ![https://ithelp.ithome.com.tw/upload/images/20250814/20119035SEH5aXgDM3.png](https://ithelp.ithome.com.tw/upload/images/20250814/20119035SEH5aXgDM3.png)
程式碼

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class CartController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $cart = DB::table('carts')->get()->first();
        if (empty($cart)) {
            DB::table('carts')->insert([
                'created_at' => now(),
                'updated_at' => now()
            ]);
            $cart = DB::table('carts')->get()->first();
        }
        $cartItems = DB::table('cart_items')
            ->where('cart_id', $cart->id)
            ->get();
        $cart = collect($cart);
        $cart['items'] = collect($cartItems);
        return response(collect($cart));
    }
}


 ![https://ithelp.ithome.com.tw/upload/images/20250814/20119035cv6sAow3Xd.png](https://ithelp.ithome.com.tw/upload/images/20250814/20119035cv6sAow3Xd.png)

-------------
用POSTMAN測試localhost:8000/carts
 
 
![https://ithelp.ithome.com.tw/upload/images/20250814/201190353IKtkpp87T.png](https://ithelp.ithome.com.tw/upload/images/20250814/201190353IKtkpp87T.png)
 
 
設定系統時間: 'timezone' => 'UTC',修改


 ![https://ithelp.ithome.com.tw/upload/images/20250814/20119035ojUaF0SfOO.png](https://ithelp.ithome.com.tw/upload/images/20250814/20119035ojUaF0SfOO.png)
 
 
改成'timezone' => 'Asia/Taipei',


![https://ithelp.ithome.com.tw/upload/images/20250814/20119035a7NasdwOnY.png](https://ithelp.ithome.com.tw/upload/images/20250814/20119035a7NasdwOnY.png)


測試確實有改過去


 ![https://ithelp.ithome.com.tw/upload/images/20250814/20119035mJFabuqbtG.png](https://ithelp.ithome.com.tw/upload/images/20250814/20119035mJFabuqbtG.png)
 

把cart_id改成2

再用POSTMAN測試顯示
這裡不知道為什麼圖片不能正常顯示~大家要看就把網址貼上就可以看到哦

大家明天見~


上一篇
後端程式碼接sql
下一篇
增加購物車裡面數量
系列文
Laravel 是甚麼30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言