mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/compatibility/mongodb-laravel-integration
MIT License
6.99k stars 1.42k forks source link

Laravel 5.4 with Moloquent give error when insert data #1200

Closed pirmax closed 4 years ago

pirmax commented 7 years ago

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

n0n0n0n0 commented 7 years ago

same problem

usamamashkoor commented 7 years ago

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

paulocoliver commented 7 years ago

same problem

rodrigo-damasceno commented 7 years ago

same problem

rodrigo-damasceno commented 7 years ago

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'
    ]
  2. 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']);
            });
        });
    }
}

/config/app.php

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