Laravel 5框架学习之数据库迁移(Migrations),laravelmigrations
database migrations 是laravel最强大的功能之一。数据库迁移可以理解为数据库的版本控制器。
在 database/migrations 目录中包含两个迁移文件,一个建立用户表,一个用于用户密码重置。
在迁移文件中,up 方法用于创建数据表,down方法用于回滚,也就是删除数据表。
执行数据库迁移
复制代码 代码如下:
php artisan migrate
#输出
Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table
查看mysql数据库,可以看到产生了三张表。 migratoins 表是迁移记录表,users 和 pasword_resets。
如果设计有问题,执行数据库回滚
复制代码 代码如下:
php artisan migrate:rollback
#输出
Rolled back: 2014_10_12_100000_create_password_resets_table
Rolled back: 2014_10_12_000000_create_users_table
再次查看mysql数据库,就剩下 migrations 表了, users password_resets 被删除了。
修改迁移文件,再次执行迁移。
新建迁移
复制代码 代码如下:
php artisan make:migration create_article_table --create='articles'
#输出
Created Migration: 2015_03_28_050138_create_article_table
在 database/migrations 下生成了新的文件。
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateArticleTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('articles', function(Blueprint $table) { $table->increments('id'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('articles'); } }
自动添加了 id列,自动增长,timestamps() 会自动产生 created_at 和 updated_at 两个时间列。我们添加一些字段:
public function up() { Schema::create('articles', function(Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('body'); $table->timestamp('published_at'); $table->timestamps(); }); }
执行迁移:
复制代码 代码如下:
php artisan migrate
现在有了新的数据表了。
假设我们需要添加一个新的字段,你可以回滚,然后修改迁移文件,再次执行迁移,或者可以直接新建一个迁移文件
复制代码 代码如下:
php artisan make:migration add_excerpt_to_articels_table
查看新产生的迁移文件
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class AddExcerptToArticelsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { // } /** * Reverse the migrations. * * @return void */ public function down() { // } }
只有空的 up 和 down 方法。我们可以手工添加代码,或者我们让laravel为我们生成基础代码。删除这个文件,重新生成迁移文件,注意添加参数:
复制代码 代码如下:
php artisan make:migration add_excerpt_to_articels_table --table='articles'
现在,up 方法里面有了初始代码。
public function up() { Schema::table('articles', function(Blueprint $table) { // }); }
添加实际的数据修改代码:
public function up() { Schema::table('articles', function(Blueprint $table) { $table->text('excerpt')->nullable(); }); } public function down() { Schema::table('articles', function(Blueprint $table) { $table->dropColumn('excerpt'); }); }
nullable() 表示字段也可以为空。
再次执行迁移并检查数据库。
如果我们为了好玩,执行回滚
复制代码 代码如下:
php artisan migrate:rollback
excerpt 列没有了。
以上所述就是本文的全部内容了,希望能够给大家熟练掌握Laravel5框架有所帮助。

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

DVWA
DVWA(Damn Vulnerable Web App)는 매우 취약한 PHP/MySQL 웹 애플리케이션입니다. 주요 목표는 보안 전문가가 법적 환경에서 자신의 기술과 도구를 테스트하고, 웹 개발자가 웹 응용 프로그램 보안 프로세스를 더 잘 이해할 수 있도록 돕고, 교사/학생이 교실 환경 웹 응용 프로그램에서 가르치고 배울 수 있도록 돕는 것입니다. 보안. DVWA의 목표는 다양한 난이도의 간단하고 간단한 인터페이스를 통해 가장 일반적인 웹 취약점 중 일부를 연습하는 것입니다. 이 소프트웨어는

에디트플러스 중국어 크랙 버전
작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

SecList
SecLists는 최고의 보안 테스터의 동반자입니다. 보안 평가 시 자주 사용되는 다양한 유형의 목록을 한 곳에 모아 놓은 것입니다. SecLists는 보안 테스터에게 필요할 수 있는 모든 목록을 편리하게 제공하여 보안 테스트를 더욱 효율적이고 생산적으로 만드는 데 도움이 됩니다. 목록 유형에는 사용자 이름, 비밀번호, URL, 퍼징 페이로드, 민감한 데이터 패턴, 웹 셸 등이 포함됩니다. 테스터는 이 저장소를 새로운 테스트 시스템으로 간단히 가져올 수 있으며 필요한 모든 유형의 목록에 액세스할 수 있습니다.

WebStorm Mac 버전
유용한 JavaScript 개발 도구
