国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

Laravel實(shí)現(xiàn)數(shù)據(jù)庫遷移與支持中文的填充

2024-05-05 00:00:54
字體:
供稿:網(wǎng)友

前言

數(shù)據(jù)庫遷移實(shí)際上就是對數(shù)據(jù)庫庫表的結(jié)構(gòu)變化做版本控制,之前對數(shù)據(jù)庫庫表結(jié)構(gòu)做修改的方式比較原始,比如說對某張庫表新增了一個字段,都是直接在庫表中執(zhí)行alter table xxx add .. 的方式直接修改,但是這么做有些弊端,比如在開發(fā)階段,你自己的庫表修改了,還要把這句sql語句傳給別人再執(zhí)行一遍,這在多人協(xié)同開發(fā)時不是一種好的方式.那有沒有一種方式能讓我們對數(shù)據(jù)庫 庫表的修改做一些簡單的版本控制,同時能讓其他人很方便的同步我們對數(shù)據(jù)庫的修改呢?

答案是我們可以使用Laravel 內(nèi)置的Migrations .

對數(shù)據(jù)庫的管理包括哪些部分?

其實(shí)Laravel對數(shù)據(jù)庫的版本管理主要包括兩部門: 數(shù)據(jù)庫結(jié)構(gòu)的管理 和數(shù)據(jù)的管理.

  • 數(shù)據(jù)庫結(jié)構(gòu)的管理: 主要是對數(shù)據(jù)庫結(jié)構(gòu)進(jìn)行管理,比如新增了一張表,某張表增加了一個字段等等.
  • 數(shù)據(jù)的管理: 這個主要是管理表中的數(shù)據(jù),生成一些填充數(shù)據(jù),解決我們開發(fā)調(diào)試時沒有測試數(shù)據(jù)的問題.

經(jīng)常我們做項目都團(tuán)隊協(xié)作開發(fā),每個人都在自己本地的數(shù)據(jù)庫,如果你曾經(jīng)出現(xiàn)過讓同事手動在數(shù)據(jù)庫結(jié)構(gòu)中添加字段的情況,數(shù)據(jù)庫遷移可以解決你這個問題。

不僅如此,在線上部署的時候,也避免了手動導(dǎo)入數(shù)據(jù)庫或手動修改數(shù)據(jù)結(jié)構(gòu)的麻煩,數(shù)據(jù)遷移幫你方便的維護(hù)著數(shù)據(jù)結(jié)構(gòu)。

數(shù)據(jù)填充,讓我們測試的時候需要大量的假數(shù)據(jù)不再一條一條的去造數(shù)據(jù),可以輕松的批量填充大量數(shù)據(jù)。

本文基于Laravel5.5,其他版本大同小異。

數(shù)據(jù)遷移

假如我們需要一張學(xué)生表,我們不再使用原生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ù)結(jié)構(gòu)

<?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');  // 學(xué)生名稱  $table->string('name');  // 性別  $table->string('sex');  // 郵箱  $table->string('email');  // 喜愛的顏色  $table->string('favorite_color');  // 手機(jī)號  $table->string('phone');  // 地址  $table->string('addr');  // 自動維護(hù)時間戳  $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('students'); }}

更多用法,請參考官方手冊。

運(yùn)行遷移

php artisan migrate

這樣會運(yùn)行database/migrations/目錄的所有遷移文件,并自動創(chuàng)建migrations表,來記錄已經(jīng)運(yùn)行過的遷移文件,防止重復(fù)運(yùn)行。

我們看一下數(shù)據(jù)庫是不是自動創(chuàng)建了students表了呢。

如果出現(xiàn)以下錯誤:

[Illuminate/Database/QueryException]SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too 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 too long; max key length is 767 bytes

在database/migrations/目錄里會有laravel自帶的用戶和重置密碼的兩個遷移文件,會一并運(yùn)行。
在這里我們這樣解決,修改數(shù)據(jù)庫配置文件config/database.php里的mysql下的字符集為utf8即可

'charset' => 'utf8','collation' => 'utf8_unicode_ci',

數(shù)據(jù)填充(支持中文)

創(chuàng)建學(xué)生表Eloquent模型

在app目錄下創(chuàng)建Student.php

<?phpnamespace App;use Illuminate/Database/Eloquent/Model;/** * 學(xué)生模型 */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() { // 調(diào)用模型工廠 生成10000條數(shù)據(jù) factory(App/Student::class, 10000)->create(); }}

調(diào)用該 Seeders

我們打開database/seeds/DatabaseSeeder.php文件,修改為

<?phpuse Illuminate/Database/Seeder;class DatabaseSeeder extends Seeder{ /** * Run the database seeds. * * @return void */ public function run() { // 調(diào)用學(xué)生表填充文件 $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填充中文

 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

接著我們運(yùn)行填充命令:

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)濟(jì)開發(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

 

……

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對VeVb武林網(wǎng)的支持。


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 五台县| 遂溪县| 海丰县| 连江县| 丰都县| 孟连| 阜新| 崇信县| 廊坊市| 铜鼓县| 遂川县| 石泉县| 阳朔县| 方城县| 舟曲县| 江孜县| 太原市| 闵行区| 扎赉特旗| 抚州市| 侯马市| 靖边县| 新建县| 通道| 同江市| 平南县| 靖宇县| 衡南县| 临夏县| 唐海县| 台南市| 太原市| 中宁县| 靖远县| 民和| 昆明市| 通化市| 灵山县| 伽师县| 扎兰屯市| 叙永县|