Demo of laravel5.1 Database migration file

Source: Internet
Author: User

<?php

Use Illuminate\database\schema\blueprint;
Use illuminate\database\migrations\migration;

Class Artical extends migration
{
This method for itself to determine whether the database index exists, $table table name, $name index name, can be called in this file using the
Public Function Hasindex ($table, $name)
{
$conn = Schema::getconnection ();
$dbSchemaManager = $conn->getdoctrineschemamanager ();
$doctrineTable = $dbSchemaManager->listtabledetails ($table);
Return $doctrineTable->hasindex ($name);
}
/**
* Run the migrations.
*
* @return void
*/
Public function up ()
{
Create a Table article table
if (! Schema::hastable (' article ')) {//Determine if the article table exists
Schema::create (' article ', function (Blueprint $table) {
$table->increments (' id ');
$table->string (' email ')->default (' [email protected] ')->nullable ()->comment (' email ');
$table->char (' password ', +)->comment (' Password ');
$table->string (' title ')->comment (' titles ');
$table->timestamps ();//Add a field for creation time and a field to modify time
$table->remembertoken ();
$table->engine = ' MyISAM ';
});
}else{
2016/3/5 Alpine
if (! Schema::hascolumn (' article ', ' Sex ')} {//Determine if the column exists and does not exist create this column
Schema::table (' article ', function ($table) {
$table->tinyinteger (' Sex ')->default (' 1 ')->comment (' Gender ');
});
}
2016/3/7 Alpine
if (Schema::hascolumn (' article ', ' title ')) {//Judgment column modifies the properties of its field if it exists
Schema::table (' article ', function ($table) {
$table->text (' title ')->change ();
});
}
2016/3/9 Alpine
if (Schema::hascolumn (' article ', ' Remember_token ')) {//If there are remember_token columns in the article table then delete
Schema::table (' article ', function ($table) {
$table->dropcolumn (' Remember_token ');
});
}


}
}

/**
* Reverse the migrations.
*
* @return void
*/
Public function Down ()
{
Delete an article table
Schema::d rop (' article ');

}
}

This article from "Mountain" blog, declined reprint!

Demo of laravel5.1 Database migration file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.