1

I am using laravel 5.6, mongodb and mysql in one of my project. I used jessengers mongodb package and through which I created schema for my 3 collections, though mongodb is schema less database but for documentation purposes I created schema. one of the example is:

<?php

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

class CreateChatMessagesTable extends Migration
{

    /**
     * The name of the database connection to use.
     *
     * @var string
     */
    protected $connection = 'mongodb';


    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::connection($this->connection)->create('chat_messages', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->integer('trade_id');
            $table->tinyInteger('type')->default('1');
            $table->text('content');
            $table->integer('recipient_id');
            $table->timestamp('recipient_read_at')->nullable();
            $table->timestamp('created_at')->nullable();
            $table->tinyInteger('status')->default('1');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::connection($this->connection)
            ->table('chat_messages', function (Blueprint $collection)
            {
                $collection->drop();
            });
    }
}

here the problem is whenever I run php artisan migrate:fresh command it gives me an error like collection already exists. I need to check, especially for mongodb, that if collection exists, then do not migrate the collection again.

I guessed that I should run a query like

if(true == ChatMessage::get()){
//do not run the migration
} else{
//continue the migration
}

in migration file only, but I never tried for this way, I kept it for last resolution. Please guide and help me.

1 Answer 1

1

I got the solution after searching the documentations of laravel.

There is one method called: hasTable() which returns boolean value whether collection/table does exist.

This is how I did and It's working fine now:

<?php

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

class CreateChatMessagesTable extends Migration
{

    /**
     * The name of the database connection to use.
     *
     * @var string
     */
    protected $connection = 'mongodb';


    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if(Schema::connection($this->connection)->hasTable('chat_messages') == false) {
            Schema::connection($this->connection)->create('chat_messages', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('user_id');
                $table->integer('trade_id');
                $table->tinyInteger('type')->default('1');
                $table->text('content');
                $table->integer('recipient_id');
                $table->timestamp('recipient_read_at')->nullable();
                $table->timestamp('created_at')->nullable();
                $table->tinyInteger('status')->default('1');
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        if(Schema::connection($this->connection)->hasTable('chat_messages') == false) {
            Schema::connection($this->connection)
                ->table('chat_messages', function (Blueprint $collection) {
                    $collection->drop();
                });
        }
    }
}

I hope someone will get benefited by this solution in future.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.