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
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:
'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'
]
Overwrite the moloquent provider to fix it. ( best in my opnion )
<?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,
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:
Overwrite the moloquent provider to fix it. ( best in my opnion )
/config/app.php
// Moloquent\MongodbServiceProvider::class,
App\Providers\MongodbServiceProvider::class,