php Laravel Artisan迁移实例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Laravel Artisan迁移实例相关的知识,希望对你有一定的参考价值。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePlotTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('plot', function (Blueprint $table) {
        
            $table->engine = 'InnoDB';

            $table->increments('id');

            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('set null');
            // $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            $table->string('name', 150)->unique();
            $table->text('address')->nullable();
            $table->string('postcode', 10);
            $table->text('description')->nullable();
            $table->string('image')->nullable();
            $table->string('thumbnail')->nullable();

            $table->string('database_host', 200)->nullable()->default('127.0.0.1');

            $table->dateTime('available_from')->nullable();

            $table->integer('status')->default(1);
            
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');
        Schema::dropIfExists('plot');
        DB::statement('SET FOREIGN_KEY_CHECKS = 1');
    }
}
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateVirtualhostTable extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create('virtualhost', function(Blueprint $table)
		{
			$table->increments('id');
			$table->string('domain_old', 191)->default('');
			$table->string('title', 50)->nullable()->index();
			$table->integer('project_id')->unsigned()->index('virtualhost_project_id_foreign');
			$table->integer('domain_id')->unsigned();
			$table->integer('stage_id')->default(1);
			$table->integer('parent_id')->nullable()->default(0);
			$table->integer('provision')->default(0);
			$table->integer('status_id')->nullable()->default(1);
			$table->integer('framework_id')->default(1);
			$table->string('node', 50)->index();
			$table->string('public_folder', 150)->nullable()->default('current/public');
			$table->integer('certificateissuer_id')->default(1);
			$table->text('certificate_domains', 65535)->nullable();
			$table->integer('environment_id')->default(1);
			$table->integer('debug')->nullable()->default(0);
			$table->string('database_host', 200)->nullable()->default('127.0.0.1');
			$table->string('database_name', 50)->nullable()->default('');
			$table->integer('database_user')->nullable();
			$table->integer('database_action')->default(1);
			$table->string('database_table_prefix', 20)->nullable();
			$table->integer('log_rotation')->default(3);
			$table->integer('log_rotate')->default(3);
			$table->string('log_filesize', 20)->default('50M');
			$table->integer('backup_user')->default(2);
			$table->string('backup_minute', 20)->default('00');
			$table->string('backup_hour', 20)->default('01');
			$table->integer('file_user')->default(1);
			$table->integer('file_group')->default(3);
			$table->integer('set_vhost_permissions')->nullable()->default(0);
			$table->integer('set_site_permissions')->nullable()->default(1);
			$table->string('cdn_url', 200)->nullable();
			$table->integer('http_auth')->default(0);
			$table->integer('pagespeed')->default(0);
			$table->integer('create_www_alias')->default(0);
			$table->integer('redirect_from_www')->default(1);
			$table->integer('redirect_to_www')->default(0);
			$table->integer('use_ssl')->default(1);
			$table->text('domain_alias', 65535)->nullable();
			$table->integer('resource_action')->default(5);
			$table->integer('directory_action')->default(5);
			$table->integer('config_action')->default(5);
			$table->integer('certificate_action')->default(5);
			$table->integer('log_action')->default(5);
			$table->integer('backup_action')->default(5);
			$table->integer('monitor_action')->default(5);
			$table->timestamps();
			$table->dateTime('provisioned_at')->nullable();
			$table->softDeletes();
		});
	}


	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop('virtualhost');
	}

}
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProfilesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->string('gender')->nullable();
            $table->date('date_of_birth')->nullable();
            $table->string('avatar')->nullable();
            $table->string('facebook_profile')->nullable();
            $table->string('twitter_profile')->nullable();
            $table->string('google_plus_profile')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('profiles');
    }
}
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class AddForeignKeysToUsersettingTable extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::table('usersetting', function(Blueprint $table)
		{
			$table->foreign('user_id')->references('id')->on('users')->onUpdate('RESTRICT')->onDelete('RESTRICT');
		});
	}


	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::table('usersetting', function(Blueprint $table)
		{
			$table->dropForeign('usersetting_user_id_foreign');
		});
	}

}
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateNotificationsTable extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create('notifications', function(Blueprint $table)
		{
			$table->char('id', 36)->primary();
			$table->string('type', 191);
			$table->string('notifiable_type', 191);
			$table->bigInteger('notifiable_id')->unsigned();
			$table->text('data', 65535);
			$table->dateTime('read_at')->nullable();
			$table->timestamps();
			$table->index(['notifiable_type','notifiable_id']);
		});
	}


	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop('notifications');
	}

}
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateDeploymentTable extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create('deployment', function(Blueprint $table)
		{
			$table->increments('id');
			$table->integer('user_id')->unsigned()->index('deployment_user_id_foreign');
			$table->integer('node_id')->unsigned()->index('deployment_node_id_foreign');
			$table->text('log', 65535)->nullable();
			$table->timestamps();
			$table->softDeletes();
			$table->integer('status_id')->default(34);
		});
	}


	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop('deployment');
	}

}

以上是关于php Laravel Artisan迁移实例的主要内容,如果未能解决你的问题,请参考以下文章

为啥我收到 Laravel php artisan 迁移错误?

Laravel 5.6.17 php artisan 迁移错误与 php 7.2 [重复]

php laravel artisan 迁移模型中的错误?

Laravel php artisan 迁移空数据库上的错误

使用“php artisan migrate”命令时 Laravel 数据库迁移外键错误

如何在 laravel 中使用“php artisan migrate”迁移我的数据库