-1

In Laravel the order in which you create your migration and seeders are essential.

When these are run in artisan, if there are any foreign keys in a class(table), and one is executed before the one that is being referenced, the execution will stop, and you get an error.

There is a class called DatabaseSeeder that is run when you call php artisan db:seed. This class includes all of your seeder classes. https://laravel.com/docs/5.7/seeding#writing-seeders

Is there any equivalent for the migrations?

<?php

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        $this->call([
            LanguageTableDataSeeder::class,
            UserTableDataSeeder::class,
            PlaceTableDataSeeder::class]);
    }
}
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Marc DG
  • 51
  • 10

1 Answers1

0

yes you can do it

But its Not Recommended

Step 1: Run the Command php artisan make:migration create_bulk_table

Now open your migration folder inside the database/migrations you will find the new migration time_stamp_create_bulk_table.php

open it You will find the Contents like this

<?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');
    }
}

How To Do it

There are two methods in this

one is for creating the table that is up METHOD

and

another one is for dropping the table that is down METHOD

For Example if You want to migrate

posts table ,tasks table ,products table

In Single Migration

<?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

For Example You have column user_id in post table Which you are refering like

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

users table needs to be migrated before the posts table

Hope it is clear

Kindly Comment below if you find any bugs

ManojKiran A
  • 5,896
  • 4
  • 30
  • 43
  • Thank you Manojkiran A.! It makes sens to do it like this. It's funny that there is no particular artisan command to make it like the `` DatabaseSeeder ``. It's so convenient! – Marc DG Feb 08 '19 at 23:45