问题
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