Laravel-mongodb: Laravel 5.4 with Moloquent give error when insert data

Created on 25 May 2017  路  5Comments  路  Source: jenssegers/laravel-mongodb

I have this error with moloquent/moloquent package.

composer require moloquent/moloquent

My Item class Model extended from Moloquent:

<?php

namespace App;

use Moloquent\Eloquent\Model as Eloquent;

class Item extends Eloquent
{
    protected $collection = 'items';
    protected $connection = 'mongodb';
...
}

My test to insert into items collection:

Item::create([
    'user_id' => 1,
    'title' => 'test',
    'slug' => 'test',
])

Result:

FatalThrowableError in Builder.php line 103: Type error: Argument 1
passed to Moloquent\Query\Builder::__construct() must be an instance
of Moloquent\Connection, instance of
Illuminate\Database\MySqlConnection given, called in
/home/site_com/http/www/vendor/moloquent/moloquent/src/Eloquent/Model.php
on line 560

question

Most helpful comment

I found a solution. This error occurs because the connection name -- inside the moloquent database config -- is missing. Actually, laravel puts the connection name automatic, but moloquent doesn't. When the connection name is not found, laravel uses the default connection configured in the file /config/database.php, in most cases it is the mysql.

You can solve this by two ways:

  1. Add connection name into mongodb config :
'mongodb' => [

        'driver'   => 'mongodb',
        'host'     => env('MONGODB_HOST', 'localhost'),
        'port'     => env('MONGODB_PORT', 27017),
        'database' => env('MONGODB_DATABASE'),
        'username' => env('MONGODB_USERNAME'),
        'password' => env('MONGODB_PASSWORD'),
        'name' => 'mongodb'
]
  1. Overwrite the moloquent provider to fix it. ( best in my opnion )

    • Create the file Providers/MongodbServiceProvider.php that extends Moloquent\MongodbServiceProvider
<?php
namespace App\Providers;

use Moloquent\MongodbServiceProvider as Base;
use Moloquent\Queue\MongoConnector;

class MongodbServiceProvider extends Base{

    public function register()
    {
        // Add database driver.
        $this->app->resolving('db', function ($db) {
            $db->extend('mongodb', function ($config, $name) {
                $config['name'] = $name;
                return new \Moloquent\Connection($config);
            });
        });

        // Add connector for queue support.
        $this->app->resolving('queue', function ($queue) {
            $queue->addConnector('mongodb', function () {
                return new MongoConnector($this->app['db']);
            });
        });
    }
}
  • Uses your provider instead of moloquent provider

/config/app.php

// Moloquent\MongodbServiceProvider::class,
App\Providers\MongodbServiceProvider::class,

All 5 comments

same problem

@pirmax and @n0n0n0n0 did you have any luck with finding solution for this problem...?

same problem

same problem

I found a solution. This error occurs because the connection name -- inside the moloquent database config -- is missing. Actually, laravel puts the connection name automatic, but moloquent doesn't. When the connection name is not found, laravel uses the default connection configured in the file /config/database.php, in most cases it is the mysql.

You can solve this by two ways:

  1. Add connection name into mongodb config :
'mongodb' => [

        'driver'   => 'mongodb',
        'host'     => env('MONGODB_HOST', 'localhost'),
        'port'     => env('MONGODB_PORT', 27017),
        'database' => env('MONGODB_DATABASE'),
        'username' => env('MONGODB_USERNAME'),
        'password' => env('MONGODB_PASSWORD'),
        'name' => 'mongodb'
]
  1. Overwrite the moloquent provider to fix it. ( best in my opnion )

    • Create the file Providers/MongodbServiceProvider.php that extends Moloquent\MongodbServiceProvider
<?php
namespace App\Providers;

use Moloquent\MongodbServiceProvider as Base;
use Moloquent\Queue\MongoConnector;

class MongodbServiceProvider extends Base{

    public function register()
    {
        // Add database driver.
        $this->app->resolving('db', function ($db) {
            $db->extend('mongodb', function ($config, $name) {
                $config['name'] = $name;
                return new \Moloquent\Connection($config);
            });
        });

        // Add connector for queue support.
        $this->app->resolving('queue', function ($queue) {
            $queue->addConnector('mongodb', function () {
                return new MongoConnector($this->app['db']);
            });
        });
    }
}
  • Uses your provider instead of moloquent provider

/config/app.php

// Moloquent\MongodbServiceProvider::class,
App\Providers\MongodbServiceProvider::class,

Was this page helpful?
0 / 5 - 0 ratings