Day 0x7 - Laravel 资料库连接设定、资料表规划

0x1 Laravel 资料库连接

请先确认 php.ini 的 pdo_pgsql extension 是否取消注解

并安装 Postgresql 13.4 >>> 官方下载位置 <<<

  1. 安装过程基本上都是下一步,预设密码则为了方便开发这里用跟帐号同样是 postgres,
  2. 安装完成後,开启 pgAdmin4,键入刚刚设定的预设密码
  3. 右键 Database -> Create -> Database...,建立一个资料库 sinopac,您也可以取自己喜欢的名称
    https://ithelp.ithome.com.tw/upload/images/20210915/20141805gAQR8csE6k.png
    https://ithelp.ithome.com.tw/upload/images/20210915/20141805igKoXFx6cH.png
  4. 建立完成後,开启专案的 .env ,找到 DB_connection 後更改设定
    DB_CONNECTION=pgsql
    DB_HOST=127.0.0.1
    DB_PORT=5432
    DB_DATABASE=sinopac
    DB_USERNAME=postgres
    DB_PASSWORD=postgres
    
  5. 执行指令,
    # 连线正常会回应 Migration table created successfully.
    $ php artisan migrate:install
    

0x2 资料表规划

先打个预防针,这里的资料表结构是现在一边打一边想出来的,所以没有专业的大神安排的好,单纯对於这个API回应的讯息有地方可以放

一个原则,不要太复杂,10分钟就想完的结构

  • 以下表格
    1. customer
      1. id 自增值
      2. first_name nvarchar(50) not null
      3. last_name nvarchar(50)
    2. sale_order
      1. id 自增值
      2. customer_id m2o customer not null 顾客_id
      3. order_no nvarchar(20) not null 订单编号
      4. total int not null 订单总金额
      5. pay_type nvarchar(5) not null 付款方式
      6. pay_datetime datetime not null 付款时间
      7. create_datetime datetime not null 建立时间
      8. status nvarchar(10) 订单状态
      9. expire_date date not null 订单过期日期
      10. mailing_address nvarchar(200) not null 寄送地址

0x3 建立资料表

备注: 资料表栏位可能会随着後面的天数增加而变动

  1. 建立 model 以及 migrate 档案

    $ php artisan make:model -m customer
    $ php artisan make:model -m sale_order
    
  2. 修改 migrate 档案,这里定义 columns

    // path: sinopac_api/database/migrations/[date]_create_customer_table.php
    public function up()
    {
        Schema::create('customers', function (Blueprint $table) {
            $table->id();
            $table->string('first_name', 50)->nullable(false);
            $table->string('last_name', 50);
            $table->timestamps();
        });
    }
    
    // path: sinopac_api/database/migrations/[date]_create_sale_orders_table.php
    public function up()
    {
        Schema::create('sale_orders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('customer_id')->nullable(false);
            $table->string('order_no', 20)->nullable(false)->comment('订单编号');
            $table->integer('total')->nullable(false)->comment('订单总金额');
            $table->string('pay_type', 5)->nullable(false)->comment('付款方式');
            $table->dateTime('pay_datetime')->nullable(false)->comment('付款时间');
            $table->string('status', 10)->comment('订单状态');
            $table->date('expire_date')->comment('订单过期日期');
            $table->string('mailing_address', 200)->nullable(false)->comment('寄送地址');
            $table->timestamps();
        });
    }
    
  3. 执行 migrate 指令,完成後就能在资料库看到 table

    $ php artisan migrate
    
  4. 设定 model fillable (这一步在 migrate 前後做皆可),增加 fillable 属性,这里我的理解是 资料建立或更新 时,允许哪几个栏位可以被更动

    // path: sinopac_api/app/Models/customer.php
    protected $fillable = [
        'first_name',
        'last_name',
    ];
    
    // path: sinopac_api/app/Models/sale_order.php
    protected $fillable = [
        'customer_id',
        'order_no',
        'total',
        'pay_type',
        'pay_datetime',
        'status',
        'expire_date',
        'mailing_address',
    ];
    
  5. 如果要整个 migrate 重跑,执行 fresh,会把所有 table drop,使用时请小心
    bash $ php artisan migrate:fresh

0x3 今日小结

对於 migrate 还蛮不熟的,看了很多文章,也问了对於 laravel 有研究的同事,在这篇花了蛮大量的时间,
工作上虽有使用 laravel,不过对於 table 的操作都是下 SQL 语法,
这还真的是第一次完整使用 migrate ;D
如果有任何解释或理解错误的部分,还望大神们不吝指教

明天预计会先把接收讯息的 api 写好,明天见


<<:  【Day 2】Google Apps Script - 平台介绍

>>:  【D3】发现厨房不能用,需要更换厨房环境: 使用Python 3.8

CISSP考试资源

CISSP快速启动版(建议之最低要求) https://wentzwu.com/cissp-expr...

LiteX/VexRiscv 简介与使用 (二) 始有昼夜

好的,来到第九天了。今天我们将来搭建Linux on LiteX/VexRiscv的建置环境。 因笔...

IT 铁人赛 k8s 入门30天 -- day20 k8s Logging Architecture

前言 参考来源: https://kubernetes.io/docs/concepts/clust...

#1-连结Hover动起来!(CSS 伪元素)

网站必备!连结动态 连结的Hover动态算是网页动态最基本款, 一个好的动态绝对可以帮网页 点击率(...

[Golang]恢复panic(recover、defer)-心智图总结

1. 如何让panic,包含一个值 在呼叫panic函数时,把某个值做为参数传给该函数就可以了。pa...