I want to work with MongoDB through Moloquent without Laravel. Installed through the composer require moloquent/moloquent , created index.php , wrote the following code:
<?php require_once( __DIR__ . '/vendor/autoload.php' ); use Illuminate\Database\Capsule\Manager as Capsule; // use Illuminate\Events\Dispatcher; // use Illuminate\Container\Container; use Moloquent\Eloquent\Model as Eloquent; $mongoconfig = [ 'driver' => 'mongodb', 'host' => 'localhost', 'port' => 27017, 'database' => 'test', 'username' => 'mongoadmin', 'password' => '12345678' 'options' => [ 'db' => 'admin' ] ]; $capsule = new Capsule; $capsule->getDatabaseManager()->extend( 'mongodb', function( $config ) { return new \Moloquent\Connection( $mongoconfig ); }); class User extends Eloquent { // protected $connection = 'mongodb'; protected $collection = 'posts'; } var_dump( User::count() ); Swears like this:
2017/07/03 05:06:48 [error] 4379#4379: *8 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Call to a member function connection() on null in /var/www/test/vendor/illuminate/database/Eloquent/Model.php:1041 Stack trace: #0 /var/www/test/vendor/illuminate/database/Eloquent/Model.php(1007): Illuminate\Database\Eloquent\Model::resolveConnection('mongodb') #1 /var/www/test/vendor/moloquent/moloquent/src/Eloquent/Model.php(558): Illuminate\Database\Eloquent\Model->getConnection() #2 /var/www/test/vendor/illuminate/database/Eloquent/Model.php(814): Moloquent\Eloquent\Model->newBaseQueryBuilder() #3 /var/www/test/vendor/illuminate/database/Eloquent/Model.php(798): Illuminate\Database\Eloquent\Model->newQueryWithoutScopes() #4 /var/www/test/vendor/illuminate/database/Eloquent/Model.php(1357): Illuminate\Database\Eloquent\Model->newQuery() #5 /var/www/test/vendor/moloquent/moloquent/src/Eloquent/Model.php(590): Illuminate\Database\Eloquent\Model->__call('count', Array) #6 /var/www/test/vendor/illuminate/database/Eloquent/Model.php(1369): Moloquent\Eloquent\M" while reading response header from upstream, client: 192.168.33.1, server: test.dev, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "test.dev" The connection is valid, the test database exists, the posts collection exists, there is 1 record for the test. What am I doing wrong?
Infu took part here and here .
PS The choice of Moloquent is not critical, but desirable. I would like to understand the future in general in the approach to such solutions, because Moloquent is far from the only module that I would like to use separately. However, in order not to go beyond the scope of this issue, I ask for help only with Moloquent, otherwise I will try to figure it out myself later, focusing on this question. Thank you for understanding.
PPS I left the commented lines to show what I tried to “play” with and did not work.