Laravel-mongodb: Migration: $collectionName is invalid

Created on 27 Dec 2016  路  4Comments  路  Source: jenssegers/laravel-mongodb

I've searched through the issues and cannot find anything that resolves this issue. I'm using 3.1.3 with Lumen 5.3.2. I have no problems connecting to and using a MongoDB database, but I cannot seem to get migrations to work.

When I try to run php artisan migrate -v, I just receive the following:

[MongoDB\Exception\InvalidArgumentException]
$collectionName is invalid:                   

Exception trace:
 () at /Users/randy/public_html/cherokee/cherokee/vendor/mongodb/mongodb/src/Collection.php:90
 MongoDB\Collection->__construct() at /Users/randy/public_html/cherokee/cherokee/vendor/mongodb/mongodb/src/Database.php:304
 MongoDB\Database->selectCollection() at /Users/randy/public_html/cherokee/cherokee/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Connection.php:91
 Jenssegers\Mongodb\Connection->getCollection() at /Users/randy/public_html/cherokee/cherokee/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Schema/Blueprint.php:39
 Jenssegers\Mongodb\Schema\Blueprint->__construct() at /Users/randy/public_html/cherokee/cherokee/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Schema/Builder.php:139
 Jenssegers\Mongodb\Schema\Builder->createBlueprint() at /Users/randy/public_html/cherokee/cherokee/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Schema/Builder.php:109
 Jenssegers\Mongodb\Schema\Builder->create() at /Users/randy/public_html/cherokee/cherokee/vendor/illuminate/database/Migrations/DatabaseMigrationRepository.php:144
 Illuminate\Database\Migrations\DatabaseMigrationRepository->createRepository() at /Users/randy/public_html/cherokee/cherokee/vendor/illuminate/database/Console/Migrations/InstallCommand.php:54
 Illuminate\Database\Console\Migrations\InstallCommand->fire() at n/a:n/a
 call_user_func_array() at /Users/randy/public_html/cherokee/cherokee/vendor/illuminate/container/Container.php:508
 Illuminate\Container\Container->call() at /Users/randy/public_html/cherokee/cherokee/vendor/illuminate/console/Command.php:169
 Illuminate\Console\Command->execute() at /Users/randy/public_html/cherokee/cherokee/vendor/symfony/console/Command/Command.php:254
 Symfony\Component\Console\Command\Command->run() at /Users/randy/public_html/cherokee/cherokee/vendor/illuminate/console/Command.php:155
 Illuminate\Console\Command->run() at /Users/randy/public_html/cherokee/cherokee/vendor/illuminate/console/Command.php:185
 Illuminate\Console\Command->call() at /Users/randy/public_html/cherokee/cherokee/vendor/illuminate/database/Console/Migrations/MigrateCommand.php:95
 Illuminate\Database\Console\Migrations\MigrateCommand->prepareDatabase() at /Users/randy/public_html/cherokee/cherokee/vendor/illuminate/database/Console/Migrations/MigrateCommand.php:58
 Illuminate\Database\Console\Migrations\MigrateCommand->fire() at n/a:n/a
 call_user_func_array() at /Users/randy/public_html/cherokee/cherokee/vendor/illuminate/container/Container.php:508
 Illuminate\Container\Container->call() at /Users/randy/public_html/cherokee/cherokee/vendor/illuminate/console/Command.php:169
 Illuminate\Console\Command->execute() at /Users/randy/public_html/cherokee/cherokee/vendor/symfony/console/Command/Command.php:254
 Symfony\Component\Console\Command\Command->run() at /Users/randy/public_html/cherokee/cherokee/vendor/illuminate/console/Command.php:155
 Illuminate\Console\Command->run() at /Users/randy/public_html/cherokee/cherokee/vendor/symfony/console/Application.php:820
 Symfony\Component\Console\Application->doRunCommand() at /Users/randy/public_html/cherokee/cherokee/vendor/symfony/console/Application.php:187
 Symfony\Component\Console\Application->doRun() at /Users/randy/public_html/cherokee/cherokee/vendor/symfony/console/Application.php:118
 Symfony\Component\Console\Application->run() at /Users/randy/public_html/cherokee/cherokee/vendor/laravel/lumen-framework/src/Console/Kernel.php:83
 Laravel\Lumen\Console\Kernel->handle() at /Users/randy/public_html/cherokee/cherokee/artisan:35
````

Looking through the code, it looks like this exception occurs when attempting to build the migration repository collection before it ever gets to my migration code.  The collection name passed to Collection::__construct() appears to be empty.

For completeness, my migration code looks like:

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

class InitializeDatabase extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::collection('users', function (Blueprint $_collection) {
$_collection->unique('username');
});
}

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

}
````

Most helpful comment

Is the same issue from #548:

  • Maybe you not configured "migrations" table name onto "config/database.php"

All 4 comments

+1

Hi @slakr007, did you managed to fix this?
I'm facing the same issue now.

No, sorry, I ultimately gave up on using MongoDB and went back to PostgreSQL. MongoDB was not working for my needs anyway.

Is the same issue from #548:

  • Maybe you not configured "migrations" table name onto "config/database.php"
Was this page helpful?
0 / 5 - 0 ratings

Related issues

viacheslavpleshkov picture viacheslavpleshkov  路  3Comments

sanjay1688 picture sanjay1688  路  3Comments

HassanIbrahim picture HassanIbrahim  路  3Comments

naveedyasin picture naveedyasin  路  3Comments

tomartailored picture tomartailored  路  3Comments