如何创建将调用所有迁移类的主迁移类?

时间:2019-02-08 02:22:21

标签: laravel laravel-migrations laravel-seeding

在Laravel中,创建迁移和种子的顺序至关重要。

当这些键在技术人员中运行时,如果一个类(表)中有任何外键,并且在被引用的外键之前执行了一个外键,则该执行将停止,并且会出现错误。

调用php artisan DatabaseSeeder时会运行一个名为db:seed的类。此类包括您的所有播种器类。 wdFormatOriginalFormatting

迁移是否等效?

<?php

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        $this->call([
            LanguageTableDataSeeder::class,
            UserTableDataSeeder::class,
            PlaceTableDataSeeder::class]);
    }
}

1 个答案:

答案 0 :(得分:0)

是的,您可以做到

但不推荐

步骤1: 运行命令php artisan make:migration create_bulk_table

现在打开database/migrations内的迁移文件夹,您将找到新的迁移time_stamp_create_bulk_table.php

打开它,您将找到类似的内容

<?php

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

class CreateBulkTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('bulk', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }

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

操作方法

此方法有两种

一个用于creating the table,即 up METHOD

另一项是针对dropping the table的,即 down METHOD

例如,如果您要迁移

posts表,tasks表,products

单次迁移

<?php

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

class CreateBulkTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        if (!Schema::hasTable('posts')) 
        {

            Schema::create('posts', function (Blueprint $table) {
                $table->increments('id');
                $table->string('post_name');
                $table->text('post_desc');
                $table->timestamps();
            });
        }


        if (!Schema::hasTable('tasks')) 
        {

            Schema::create('tasks', function (Blueprint $table) {
                $table->increments('id');
                $table->string('task_name');
                $table->enum('task_status', ['Open', 'Closed','Inactive']);
                $table->text('task_desc');
                $table->timestamps();
            });
        }


        if (!Schema::hasTable('products')) 
        {

            Schema::create('products', function (Blueprint $table) {
                $table->increments('id');
                $table->string('product_name');
                $table->text('product_desc');
                $table->string('product_price');
                $table->timestamps();
            });


        }


    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
        Schema::dropIfExists('tasks');
        Schema::dropIfExists('products');

    }
}

BE CAREFUL WHILE REFERING THE FORIEGN KEY OF PARENT TABLE WHICK NEEDS TO BE CREATED BEFORE THE REFERAL

例如,您在引用的帖子表中有user_id

 $table->foreign('user_id')->references('id')->on('users');

users表必须在posts表之前迁移

希望很清楚

如果发现任何错误,请在下面评论