Laravel-mongodb: Schema and migrations how to?

Created on 8 Jun 2016  路  3Comments  路  Source: jenssegers/laravel-mongodb

Using Lumen how do I create a migration file? I was using artisan to generate the migration file which looks like

<?php

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

class UsersCollection extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function($collection)
        {
            $collection->index('name');
            $collection->password('password');
            $collection->unique('email');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

than I try to migrate

php artisan migrate

and I get the following error

  [Illuminate\Database\QueryException]                                                                                                                                   
  SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected (SQL: create table `migrations` (`migration` varchar(255) not null, `batch` int not null) default ch  
  aracter set utf8 collate utf8_unicode_ci)                                                                                                                              



  [PDOException]                                                    
  SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected  

it seems like in this case wants to access the Mysql driver, however I have the right config under config/database.php

I do not find a proper description how should be done in this case

Most helpful comment

What I did:

<?php

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

class CreateActionsTable 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)
        ->table('actions', function (Blueprint $collection) 
        {
            $collection->index('name');
        });
    }

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

the just

php artisan migrate

... and it worked.

All 3 comments

What I did:

<?php

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

class CreateActionsTable 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)
        ->table('actions', function (Blueprint $collection) 
        {
            $collection->index('name');
        });
    }

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

the just

php artisan migrate

... and it worked.

How do you delete only the index added in the up method, instead of dropping the whole collection?

@Oyelaking Too late for you, but info for future visitors. To remove index just use:

$collection->dropIndex();

Was this page helpful?
0 / 5 - 0 ratings

Related issues

YSimple picture YSimple  路  3Comments

phuocduy1988 picture phuocduy1988  路  3Comments

sanjay1688 picture sanjay1688  路  3Comments

Idnan picture Idnan  路  3Comments

tomartailored picture tomartailored  路  3Comments