iT邦幫忙

2025 iThome 鐵人賽

DAY 12
0
Modern Web

Laravel 是甚麼系列 第 12

後端程式碼接sql

  • 分享至 

  • xImage
  •  

今天台南颱風假~
可以將前面幾篇再優化一下

Migration資料庫的變動要跟所有人說時,快速建立資料庫在正式區
可以參考:https://ithelp.ithome.com.tw/articles/10243718
先增加資料庫laravel_demo按存檔
https://ithelp.ithome.com.tw/upload/images/20250813/20119035c9dkymThZX.png

在mysql裡面也增加laravel_demo資料庫
CREATE SCHEMA laravel_demo DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ;
https://ithelp.ithome.com.tw/upload/images/20250813/20119035HsSDVFP6wX.png
顯示
https://ithelp.ithome.com.tw/upload/images/20250813/20119035jH7uxbRcHG.png
到 terminal填入php artisan make:migration create_products
顯示
https://ithelp.ithome.com.tw/upload/images/20250813/20119035wzfc5i3QXq.png
點開顯示
https://ithelp.ithome.com.tw/upload/images/20250813/20119035gBHeEa2mlJ.png
Up表示更新程式
down表示復原程式

程式碼

<?php

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

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

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

加入欄位
https://ithelp.ithome.com.tw/upload/images/20250813/20119035J7Tq4SeFVJ.png
填入:
https://ithelp.ithome.com.tw/upload/images/20250813/20119035EdyVWypd5u.png
在terminal打入php artisan migrate
顯示
https://ithelp.ithome.com.tw/upload/images/20250813/20119035ikTTv6kwEU.png
到資料庫 重新整理>看
https://ithelp.ithome.com.tw/upload/images/20250813/20119035ArOjsMv4gH.png
按 把手 顯示 datatype

https://ithelp.ithome.com.tw/upload/images/20250813/20119035F15TYmUz2S.png
購物車網站的架構
建立購物車的資料庫
php artisan make:migration create_carts_and_cart_items
顯示

<?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_and_cart_items', function (Blueprint $table) {
           $table->id();
           $table->timestamps();
       });
   }

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

變成carts跟cart_items兩個表格
https://ithelp.ithome.com.tw/upload/images/20250813/201190359tGzXulicE.png
大家明天見


上一篇
資料庫關聯
下一篇
開始建購物車
系列文
Laravel 是甚麼30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言