相關參考
建立資料填充
這裡不展示其他程式碼了,資料填充首先建立
Seeder
檔案,該檔案繼承Seeder
這裡以客戶表為例
php artisan make:seeder CustomerTableSeeder
編寫填充程式碼
<?php
use App\Models\Customer;
use Illuminate\Database\Seeder;
class CustomerTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* [@return](https://learnku.com/users/31554) void
*/
public function run()
{
$now = now()->format('Y-m-d H:i:s');
$insert = [
[Customer::NAME => 'Kathy', Customer::CITY => 'Vestal', Customer::VISITS_MADE => 2, Customer::LAST_VISIT_TIME=> '2017-11-28 10:25:32', Customer::CREATED_AT=>$now, Customer::UPDATED_AT=>$now],
[Customer::NAME => 'Brown', Customer::CITY => 'Binghamton', Customer::VISITS_MADE => 1, Customer::LAST_VISIT_TIME=> '2013-12-05 09:12:30', Customer::CREATED_AT=>$now, Customer::UPDATED_AT=>$now],
[Customer::NAME => 'Anne', Customer::CITY => 'Vestal', Customer::VISITS_MADE => 1, Customer::LAST_VISIT_TIME=> '2016-11-29 14:30:00', Customer::CREATED_AT=>$now, Customer::UPDATED_AT=>$now],
[Customer::NAME => 'Jack', Customer::CITY => 'Vestal', Customer::VISITS_MADE => 1, Customer::LAST_VISIT_TIME=> '2016-12-04 16:48:02', Customer::CREATED_AT=>$now, Customer::UPDATED_AT=>$now],
[Customer::NAME => 'Mike', Customer::CITY => 'Binghamton', Customer::VISITS_MADE => 1, Customer::LAST_VISIT_TIME=> '2016-11-30 11:52:16', Customer::CREATED_AT=>$now, Customer::UPDATED_AT=>$now],
];
Customer::query()->insert($insert);
}
}
其他的檔案填充也是如此,這裡使用了模型檔案很方便地完成了編寫
新增編寫的 Seeder 檔案到 DatabaseSeeder
新增之後就可以使用命令一次性填充資料了,當然還可以在新配置專案時直接遷移資料表和填充資料,具體請參考文件內容
這裡已經將主鍵改為bigint自增型別,作業中的
c10
字串作為主鍵不利於管理
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* [@return](https://learnku.com/users/31554) void
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(CustomerTableSeeder::class);
$this->call(EmployeeTableSeeder::class);
$this->call(SupplierTableSeeder::class);
$this->call(ProductTableSeeder::class);
$this->call(PurchaseTableSeeder::class);
}
}
執行資料填充
php artisan seed
接下來我們要編寫路由