Using the MongoDB database in the Laravel framework

Source: Internet
Author: User
Tags mongodb

Use MongoDB in the Laravel framework, provided you have installed the MongoDB and MongoDB PHP extensions.
Not installed, specific can refer to the blog http://blog.csdn.net/turtleo/article/details/50119459
1, the use of composer in the Laravel framework to introduce MongoDB

Composer require Jenssegers/mongodb

2. Edit config/app.php file
Add the following code in the providers

Jenssegers\mongodb\mongodbserviceprovider::class,

Add the following code in the aliases

' Mongo '     => jenssegers\mongodb\mongodbserviceprovider::class,

3. Configure config/database.php

' Default ' => env (' Db_connectiond ', ' MongoDB ')
' MongoDB ' => [
            ' driver '   => ' MongoDB ',
            ' host '     => ' localhost ',
            ' Port '     => 27017,
            ' database ' => ' test ',
            ' username ' => ', 
            ' password ' => 


        ',

4, the test configuration is correct
Create a new model file

<?php

namespace App;

Use Mongo;
Use DB; 

Class Mongodb extends Mongo {

    protected $collection = ' users ';
    Protected $connection = ' test ';

    public static function test () {
        $users = db::collection (' users ')->get ();
        Var_dump ($users);
    }   

Create a new controller file

<?php

namespace App\http\controllers;

Use App\http\controllers\controller;
Use App\mongodb;

Class Mongodbcontroller extends controller{

    protected function test () {
        mongodb::test ();

    }   


}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.