相關參考
資料庫配置
在 Laravel 專案的 .env 檔案中配置資料庫,在配置前先在資料庫裡新建db_learn
的資料庫,當然可以自己命名
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_learn
DB_USERNAME=root
DB_PASSWORD=123456
建立遷移檔案
在Laravel專案下使用如下
php artisan
命令,這些命令需要在php
環境下被使用,使用前請先完成環境搭建
php artisan make:migration create_employees_table
php artisan make:migration create_customers_table
php artisan make:migration create_suppliers_table
php artisan make:migration create_products_table
php artisan make:migration create_purchases_table
php artisan make:migration create_logs_table
主要部分
員工表
public function up() { Schema::create('emplyees', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('ename')->nullable(); $table->string('city')->nullable(); $table->timestamps(); }); }
客戶表
public function up() { Schema::create('customers', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('cname')->nullable(); $table->string('city')->nullable(); $table->integer('visits_made')->nullable(); $table->dateTime('last_visit_time')->nullable(); $table->timestamps(); }); }
供應商表
public function up() { Schema::create('suppliers', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('sname')->unique(); $table->string('city')->nullable(); $table->string('telephone_no')->nullable(); $table->timestamps(); }); }
產品表
public function up() { Schema::create('products', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('pname'); $table->integer('qoh'); $table->integer('qoh_threshold')->nullable(); $table->decimal('original_price',8,2)->nullable(); $table->decimal('discnt_rate',5,2)->nullable(); $table->string('sid',2)->nullable(); $table->timestamps(); }); }
購買記錄表
public function up() { Schema::create('purchases', function (Blueprint $table) { $table->bigIncrements('id'); $table->bigInteger('cid'); $table->bigInteger('eid'); $table->bigInteger('pid'); $table->integer('qty')->nullable(); $table->dateTime('ptime')->nullable(); $table->decimal('total_price',9,2); $table->timestamps(); }); }
日誌表
public function up() { Schema::create('logs', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('who'); $table->dateTime('time'); $table->string('table_name'); $table->string('operation'); $table->string('key_value'); $table->timestamps(); }); }
執行遷移
php artisan migrate
到這裡我們完成了對資料表的建立,接下來我們就要去完成資料填充