Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate laravel migration file to mongodb

I fairly new to laravel and a beginner in monogo db. I have been trying to connect mongodb cluster of mongodb atlas in my laravel project. But when I am trying to migrate the laravel migration file it is showing error saying mysql error even after I changed the default connection to mongodb. Can anyone please tell me how can I fix this issue and migrate the current project to mongodb?

PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=homestead", "homestead", "secret", [])

  1   PDOException::("SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
")
      C:\Users\admin\Desktop\test\test\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php : 68

      C:\Users\admin\Desktop\test\test\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php : 68

Since laravel doesnt allow mongodb out of the box, so I am using a mongodb package https://github.com/jenssegers/laravel-mongodb And I also would like to mention that I have monngodb installed in my php as per the documentation. I can see the confirmation of mongodb on phpinfo() page. My settings are as follows:

My .env

        DB_CONNECTION="mongodb"
        DB_MONGO_PORT=27017
        DB_MONGO_DATABASE=test
        DB_MONGO_DSN="mongodb+srv://<USERNAME>:<PASSWORD>@cluster0-
***.mongodb.net/test"

My config/database.php

 'default' => env('DB_CONNECTION', 'mongodb'),

'mongodb' => [
            'driver'   => 'mongodb',
            'dsn' => env('DB_MONGO_DSN'),
            'database' => env('DB_MONGO_DATABASE'),
        ],

My user migration file

use Illuminate\Support\Facades\Schema;
//use Illuminate\Database\Schema\Blueprint;
use Jenssegers\Mongodb\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{

    protected $connection = 'mongodb';
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
}
like image 474
user7747472 Avatar asked Dec 24 '22 09:12

user7747472


1 Answers

I'm not sure why it's not working with the default connection set to mongodb, but I've ran into this problem before. The issue I was having is that the connection property in the migration is useless. I had to do the following;

Schema::connection($this->connection)->create('collections', function (Blueprint $table) {
    $table->increments('id');
    $table->index('slug');
    $table->index('world_id');
    $table->unique(['world_id', 'slug']);
    $table->timestamps();
});

Schema::connection('mongodb') should work for you.

like image 88
ollieread Avatar answered Jan 07 '23 14:01

ollieread