Laravel数据库迁移和填充(支持中文)
寫在前面
經(jīng)常我們做項目都團隊協(xié)作開發(fā),每個人都在自己本地的數(shù)據(jù)庫,如果你曾經(jīng)出現(xiàn)過讓同事手動在數(shù)據(jù)庫結構中添加字段的情況,數(shù)據(jù)庫遷移可以解決你這個問題。
不僅如此,在線上部署的時候,也避免了手動導入數(shù)據(jù)庫或手動修改數(shù)據(jù)結構的麻煩,數(shù)據(jù)遷移幫你方便的維護著數(shù)據(jù)結構。
數(shù)據(jù)填充,讓我們測試的時候需要大量的假數(shù)據(jù)不再一條一條的去造數(shù)據(jù),可以輕松的批量填充大量數(shù)據(jù)。
本文基于Laravel5.5,其他版本大同小異。
數(shù)據(jù)遷移
假如我們需要一張學生表,我們不再使用原生SQl語句去創(chuàng)建表。
創(chuàng)建遷移文件
前提是已經(jīng)配置好了數(shù)據(jù)庫連接信息
php artisan make:migration create_students_table此命令會在database/migrations/目錄生成類似2017_10_28_035802_create_students_table.php的文件
我們在里邊添加students表的數(shù)據(jù)結構
<?phpuse Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema;class CreateStudentsTable extends Migration {/*** Run the migrations.** @return void*/public function up(){// students為表名稱Schema::create('students', function (Blueprint $table) {// 存儲引擎$table->engine = 'InnoDB';// id自增$table->increments('id');// 學生名稱$table->string('name');// 性別$table->string('sex');// 郵箱$table->string('email');// 喜愛的顏色$table->string('favorite_color');// 手機號$table->string('phone');// 地址$table->string('addr');// 自動維護時間戳$table->timestamps();});}/*** Reverse the migrations.** @return void*/public function down(){Schema::dropIfExists('students');} }更多用法,請參考官方手冊。
運行遷移
php artisan migrate這樣會運行database/migrations/目錄的所有遷移文件,并自動創(chuàng)建migrations表,來記錄已經(jīng)運行過的遷移文件,防止重復運行。
我們看一下數(shù)據(jù)庫是不是自動創(chuàng)建了students表了呢。
如果出現(xiàn)以下錯誤:
[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was t oo long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was t oo long; max key length is 767 bytes在database/migrations/目錄里會有l(wèi)aravel自帶的用戶和重置密碼的兩個遷移文件,會一并運行。
在這里我們這樣解決,修改數(shù)據(jù)庫配置文件config/database.php里的mysql下的字符集為utf8即可
想知道為什么,可猛戳 https://segmentfault.com/a/11...
數(shù)據(jù)填充(支持中文)
創(chuàng)建學生表Eloquent模型
在app目錄下創(chuàng)建Student.php
<?php namespace App;use Illuminate\Database\Eloquent\Model;/*** 學生模型*/ class Student extends Model {}創(chuàng)建填充文件
php artisan make:seed StudentsTableSeeder這條命令會在database/seeds/目錄下生成StudentsTableSeeder.php填充文件
<?phpuse Illuminate\Database\Seeder;class StudentsTableSeeder extends Seeder {/*** Run the database seeds.** @return void*/public function run(){// 調用模型工廠 生成10000條數(shù)據(jù)factory(App\Student::class, 10000)->create();} }調用該 Seeders
我們打開database/seeds/DatabaseSeeder.php文件,修改為
<?phpuse Illuminate\Database\Seeder;class DatabaseSeeder extends Seeder {/*** Run the database seeds.** @return void*/public function run(){// 調用學生表填充文件$this->call(StudentsTableSeeder::class);} }創(chuàng)建 模型工廠 填充
php artisan make:factory StudentsFactory -m Student此命令會在database/factories/目錄下生成StudentsFactory.php文件,我們定義一下要填充的數(shù)據(jù)格式
<?phpuse Faker\Generator as Faker;/* @var Illuminate\Database\Eloquent\Factory $factory */$factory->define(App\Student::class, function (Faker $faker) {$sex = rand(1, 1000);return ['name' => $faker->name,'sex' => $sex % 2 == 0 ? '男' : '女','email' => $faker->unique()->safeEmail,'favorite_color' => $faker->safeColorName,'phone' => $faker->phoneNumber,'addr' => $faker->address,]; });更多配置請查閱 vendor/fzaninotto/faker/src/Faker/Generator.php文件
讓faker填充中文
在app/Providers/AppServiceProvider.php的boot()中添加:
public function boot(){// 填充中文數(shù)據(jù)$this->app->singleton(\Faker\Generator::class, function () {return \Faker\Factory::create('zh_CN');});}開始填充
首先我們執(zhí)行一下:
composer dump-autoload自動加載一下我們在database/seeds/目錄創(chuàng)建的填充文件,以避免出現(xiàn)以下錯誤:
[ReflectionException] Class StudentsTableSeeder does not exist接著我們運行填充命令:
php artisan db:seed由于我們填充的是一萬條數(shù)據(jù),可以時間稍長,可以刷新數(shù)據(jù)庫看著逐條增加的數(shù)據(jù)。
大功告成
如果以上操作都沒有報錯的話,來看一下我們的數(shù)據(jù)庫表students表是否有數(shù)據(jù)了呢?
id | name | sex | email | favorite_color | phone | addr | created_at |updated_at
---|------|------|------|------|------|------|------|------|------|---
10000 |談英 |男 |cum_et@example.com |白色 |17642207316 |貴陽海陵區(qū) |2017-10-28 05:19:10 |2017-10-28 05:19:10
9999 |湯淑珍 |男 |qlaudantium@example.net |黑色 |18239453935 |南寧友好區(qū) |2017-10-28 05:19:10 |2017-10-28 05:19:10
9998 |賈春梅 |男 |ea35@example.com |粟色 |17103645128 |長沙蕭山區(qū) |2017-10-28 05:19:10 |2017-10-28 05:19:10
9997 |季志明 |男 |cdeleniti@example.com |灰色 |17002359608 |天津花溪區(qū) |2017-10-28 05:19:10 |2017-10-28 05:19:10
9996 |成燕 |男 |aspernatur.aut@example.com |黃色 |17181193397 |貴陽錫山區(qū) 2017-10-28 05:19:|10 |2017-10-28 05:19:10
9995 |米博 |男 |reprehenderit_autem@example.com |紫 |17187328893 |廣州東麗區(qū) |2017-10-28 05:19:10 |2017-10-28 05:19:10
9994 |蘭淑蘭 |女 |et_ea@example.com |綠色 |18592254358 |蘭州經(jīng)濟開發(fā)新區(qū) |2017-10-28 05:19:10 |2017-10-28 05:19:10
9993 |樂瑤 |女 |vel.vitae@example.org |藏青 |15891490007 |香港龍?zhí)秴^(qū) 2017-10-28 05:19:|10 |2017-10-28 05:19:10
9992 |葉志新 |女 |lcumque@example.net |藏青 |15564391466 |北京高明區(qū) |2017-10-28 05:19:10 |2017-10-28 05:19:10
9991 |胥楊 |男 |voluptatem00@example.com |黃色 |17097722096 |鄭州新城區(qū) |2017-10-28 05:19:10 |2017-10-28 05:19:10
9990 |凌敏 |女 |magni22@example.org |鮮綠色 |13021578051 |杭州涪城區(qū) |2017-10-28 05:19:10 |2017-10-28 05:19:10
9989 |席建 |女 |fugiat_accusantium@example.net |紫 |18070573726 |南昌海陵區(qū) |2017-10-28 05:19:10 |2017-10-28 05:19:10
9988 |聶新華 |女 |debitis_sapiente@example.com |水色 |17004061646 |成都南長區(qū) |2017-10-28 05:19:10 |2017-10-28 05:19:10
……
原文 https://www.tech1024.cn/origi...
總結
以上是生活随笔為你收集整理的Laravel数据库迁移和填充(支持中文)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做梦梦到猪和老鼠是什么意思
- 下一篇: 梦到脸脱皮是什么预兆