Tomato
Tomato

Reputation: 779

"Class 'MongoDB\Driver\Manager' not found"


I'm using Laravel and MongoDb with Xampp on my Mac.
I tried to make a small demo about Laravel with MongoDb like read data from MongoDb and show to view. Here is my model:

namespace App;

use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use DB;

class account extends Eloquent
{
    
    public function getaccount(){
        $acc = DB::connection('mongodb')->collection('Account')->get();

        return $acc;
    }
}

And I call this model function in my controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\account;

class AccountController extends Controller
{
    public function index() {
        $account = new account();

        $data = $account -> getaccount();

        return view ('welcome', $data);
    }
}

And read result in my view:

@if(isset($data))
  @foreach($data as $dataValue)
  <a href="https://laravel.com/docs">{{ $dataValue -> avail_balance }}</a>
  @endforeach
@endif

Final is my route web.php:

Route::get('/', 'AccountController@index');

But when I run, I received this error: "Class 'MongoDB\Driver\Manager' not found". I installed MongoDb Driver with "sudo pecl install mongodb" and have extension=mongodb.so in my php.ini file.
When I use <?php phpinfo() file to check, it don't have mongo section, but when I run php -m in my command line, it's have mongodb.
I really don't know how to fix.
Can you help me?

Upvotes: 2

Views: 959

Answers (1)

user6940918
user6940918

Reputation:

You need to specify which collection to be used for your Model

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Article extends Eloquent
{
    protected $connection = 'mongodb';
    protected $collection = 'articles';
}

Upvotes: 0

Related Questions