请先确认 php.ini 的 pdo_pgsql extension 是否取消注解
并安装 Postgresql 13.4 >>> 官方下载位置 <<<
Database
-> Create
-> Database...
,建立一个资料库 sinopac
,您也可以取自己喜欢的名称DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=sinopac
DB_USERNAME=postgres
DB_PASSWORD=postgres
# 连线正常会回应 Migration table created successfully.
$ php artisan migrate:install
先打个预防针,这里的资料表结构是现在一边打一边想出来的,所以没有专业的大神安排的好,单纯对於这个API回应的讯息有地方可以放
一个原则,不要太复杂,10分钟就想完的结构
建立 model 以及 migrate 档案
$ php artisan make:model -m customer
$ php artisan make:model -m sale_order
修改 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();
});
}
执行 migrate 指令,完成後就能在资料库看到 table
$ php artisan migrate
设定 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',
];
如果要整个 migrate 重跑,执行 fresh,会把所有 table drop,使用时请小心
bash $ php artisan migrate:fresh
对於 migrate 还蛮不熟的,看了很多文章,也问了对於 laravel 有研究的同事,在这篇花了蛮大量的时间,
工作上虽有使用 laravel,不过对於 table 的操作都是下 SQL 语法,
这还真的是第一次完整使用 migrate ;D
如果有任何解释或理解错误的部分,还望大神们不吝指教
明天预计会先把接收讯息的 api 写好,明天见
<<: 【Day 2】Google Apps Script - 平台介绍
>>: 【D3】发现厨房不能用,需要更换厨房环境: 使用Python 3.8
CISSP快速启动版(建议之最低要求) https://wentzwu.com/cissp-expr...
好的,来到第九天了。今天我们将来搭建Linux on LiteX/VexRiscv的建置环境。 因笔...
前言 参考来源: https://kubernetes.io/docs/concepts/clust...
网站必备!连结动态 连结的Hover动态算是网页动态最基本款, 一个好的动态绝对可以帮网页 点击率(...
1. 如何让panic,包含一个值 在呼叫panic函数时,把某个值做为参数传给该函数就可以了。pa...