Skip to content

Instantly share code, notes, and snippets.

@nutch31
Created January 4, 2019 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nutch31/664f52c01c7f7a006c986c68b3c01257 to your computer and use it in GitHub Desktop.
Save nutch31/664f52c01c7f7a006c986c68b3c01257 to your computer and use it in GitHub Desktop.
ERROR
ENV.
APP_ENV=local
APP_DEBUG=true
APP_KEY=e2eUQiyS5FnQCLdQdrOXJRINkpNxYOTi
APP_TIMEZONE=UTC
LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=
DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=27017
DB_DATABASE=logservice
DB_USERNAME=
DB_PASSWORD=
CACHE_DRIVER=file
QUEUE_DRIVER=sync
vendor\laravel\lumen-framework\config\database.php ใส่อันนี้ไปล่ะใน connections
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'database' => 'admin' // sets the authentication database required by mongo 3
]
],
web.php
$router->post('/post', 'Api\TestController@post');
ใน bootstrap\app.php
$app->register(Jenssegers\Mongodb\MongodbServiceProvider::class);
$app->withEloquent();
ใน model
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Test extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'tests';
protected $primarykey = 'id';
//
}
ใน controller
<?php
namespace App\Http\Controllers\Api;
use Laravel\Lumen\Routing\Controller as BaseController;
use App\Model\Test;
class TestController extends BaseController
{
public function index()
{
return 'Index';
}
public function get()
{
$test = Test::all();
return $test;
}
public function post()
{
$test = new Test;
$test->name = '1234567';
$test->save();
return $test;
}
}
ERROR ตอนเรียก post, get
{"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Argument 3 passed to MongoDB\\Driver\\Server::executeQuery() must be an instance of MongoDB\\Driver\\ReadPreference or null, array given at /var/www/html/logService/vendor/mongodb/mongodb/src/Operation/Find.php:299)
[stacktrace]
#0 /var/www/html/logService/vendor/mongodb/mongodb/src/Operation/Find.php(299): MongoDB\\Driver\\Server->executeQuery('logservice.test...', Object(MongoDB\\Driver\\Query), Array)
#1 /var/www/html/logService/vendor/mongodb/mongodb/src/Collection.php(624): MongoDB\\Operation\\Find->execute(Object(MongoDB\\Driver\\Server))
#2 [internal function]: MongoDB\\Collection->find(Array, Array)
#3 /var/www/html/logService/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Collection.php(45): call_user_func_array(Array, Array)
#4 /var/www/html/logService/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Query/Builder.php(394): Jenssegers\\Mongodb\\Collection->__call('find', Array)
#5 /var/www/html/logService/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Query/Builder.php(211): Jenssegers\\Mongodb\\Query\\Builder->getFresh(Array)
#6 /var/www/html/logService/vendor/illuminate/database/Eloquent/Builder.php(481): Jenssegers\\Mongodb\\Query\\Builder->get(Array)
#7 /var/www/html/logService/vendor/illuminate/database/Eloquent/Builder.php(465): Illuminate\\Database\\Eloquent\\Builder->getModels(Array)
#8 /var/www/html/logService/vendor/illuminate/database/Eloquent/Model.php(425): Illuminate\\Database\\Eloquent\\Builder->get(Array)
#9 /var/www/html/logService/app/Http/Controllers/Api/TestController.php(16): Illuminate\\Database\\Eloquent\\Model::all()
#10 [internal function]: App\\Http\\Controllers\\Api\\TestController->get()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment