Laravel 5框架学习之数据库迁移(Migrations)
本文给大家介绍的是Laravel5框架中最强大的功能之一数据库迁移(database migrations),本文详细给大家介绍数据库迁移的步骤和方法,非常实用,有需要的小伙伴
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 下生成了新的文件。
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
查看新产生的迁移文件
只有空的 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框架有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Flutter are popular technologies for mobile development. Flutter excels in cross-platform capabilities, performance and user interface, and is suitable for applications that require high performance, cross-platform and customized UI. PHP is suitable for server-side applications with lower performance and not cross-platform.

Database operations in PHP are simplified using ORM, which maps objects into relational databases. EloquentORM in Laravel allows you to interact with the database using object-oriented syntax. You can use ORM by defining model classes, using Eloquent methods, or building a blog system in practice.

PHP unit testing tool analysis: PHPUnit: suitable for large projects, provides comprehensive functionality and is easy to install, but may be verbose and slow. PHPUnitWrapper: suitable for small projects, easy to use, optimized for Lumen/Laravel, but has limited functionality, does not provide code coverage analysis, and has limited community support.

Laravel - Artisan Commands - Laravel 5.7 comes with new way of treating and testing new commands. It includes a new feature of testing artisan commands and the demonstration is mentioned below ?

The latest versions of Laravel 9 and CodeIgniter 4 provide updated features and improvements. Laravel9 adopts MVC architecture and provides functions such as database migration, authentication and template engine. CodeIgniter4 uses HMVC architecture to provide routing, ORM and caching. In terms of performance, Laravel9's service provider-based design pattern and CodeIgniter4's lightweight framework give it excellent performance. In practical applications, Laravel9 is suitable for complex projects that require flexibility and powerful functions, while CodeIgniter4 is suitable for rapid development and small applications.

Compare the data processing capabilities of Laravel and CodeIgniter: ORM: Laravel uses EloquentORM, which provides class-object relational mapping, while CodeIgniter uses ActiveRecord to represent the database model as a subclass of PHP classes. Query builder: Laravel has a flexible chained query API, while CodeIgniter’s query builder is simpler and array-based. Data validation: Laravel provides a Validator class that supports custom validation rules, while CodeIgniter has less built-in validation functions and requires manual coding of custom rules. Practical case: User registration example shows Lar

PHP Unit and Integration Testing Guide Unit Testing: Focus on a single unit of code or function and use PHPUnit to create test case classes for verification. Integration testing: Pay attention to how multiple code units work together, and use PHPUnit's setUp() and tearDown() methods to set up and clean up the test environment. Practical case: Use PHPUnit to perform unit and integration testing in Laravel applications, including creating databases, starting servers, and writing test code.

For beginners, CodeIgniter has a gentler learning curve and fewer features, but covers basic needs. Laravel offers a wider feature set but has a slightly steeper learning curve. In terms of performance, both Laravel and CodeIgniter perform well. Laravel has more extensive documentation and active community support, while CodeIgniter is simpler, lightweight, and has strong security features. In the practical case of building a blogging application, Laravel's EloquentORM simplifies data manipulation, while CodeIgniter requires more manual configuration.
