Why MongoDB Class doesn't work in Laravel?

若如初见. 提交于 2019-12-24 07:59:56

问题


I want to use MongoDB in Laravel, This is my code in Controller :

public function create (Request $request) {
    $m = new MongoClient();
    $db = $m->selectDB("Laravel");      
    $collection = $db->selectCollection("Posts");
    $document = array( 
       "Title" => $request->input('Title'), 
       "Publisher" => $request->input('Publisher')
    );      
    $collection->insert($document);
}    

But when I click "Submit", It gives me an error :

Fatal error: Class 'App\Http\Controllers\MongoClient' not found

I have run MongoDB Server & also Wamp & I have Installed MongoDB & It's Driver for PHP ...

What is My Mistake ? (I should say that I haven't done any config for Laravel by using MongoDB, Is it needed ?)


回答1:


Laravel doesn't come with a driver for MongoDB.

If you want to use mongo with laravel you'll have to create your own class to deal with it or use this one:

laravel-mongodb



来源:https://stackoverflow.com/questions/38790411/why-mongodb-class-doesnt-work-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!