發表文章

目前顯示的是有「反向」標籤的文章

laravel 既有資料的 seed 產生器

laravel 的 migrate 工具可以讓我們產生相應的資料庫,讓使用者輸入資料到資料庫中。 有別於透過 sql 檔案來匯入資料庫中的資料,laravel 提供了 (db:)seed 讓我們把資料匯入資料庫中。 那要怎麼產生 seed 要用的內容呢? 這裏指的是要把預設的、指定的資料內容填入資料庫,而不是用 factory 來產生隨機資料的情境。 既然有 seed 指令就是可以手寫相關的檔案,就像是 https://laravel.tw/docs/5.2/seeding 提到的,建個 seed 檔案來填相應欄位的資料,再用 DatabaseSeeder.php 來呼叫所有的 seed 檔,或是透過指令 php artisan db:seed --class=UserTableSeeder 來執行指定的 seed 檔。 想當然爾「不想手動」,那就來找個工具。 工作環境:   laravel 7.*   iseed 2.6.4 https://github.com/orangehill/iseed   安裝 iseed: $composer require orangehill/iseed 產生 seed 檔/匯出資料庫中的資料: $php artisan iseed my_table,another_table,users database/seeds 下會生成相對應的 seed 檔案 匯入 users 資料庫中的資料: $php artisan db:seed --class=UserTableSeeder 或是匯入所有的資料: $php artisan db:seed 如果安裝時出現記憶體不足 Fatal error: Allowed memory size of ########## bytes exhausted 請見這裏