PHP code example of symfomany / laravel-mongo

1. Go to this page and download the library: Download symfomany/laravel-mongo library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

symfomany / laravel-mongo example snippets


Mongo\Mongodb\MongodbServiceProvider::class,

$app->register('Mongo\Mongodb\MongodbServiceProvider');

$app->withEloquent();

$capsule->getDatabaseManager()->extend('mongodb', function($config)
{
    return new Mongo\Mongodb\Connection($config);
});

'default' => env('DB_CONNECTION', 'mongodb'),

'mongodb' => array(
    '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' => array(
        'db' => 'admin' // sets the authentication database 

'mongodb' => array(
    'driver'   => 'mongodb',
    'host'     => array('server1', 'server2'),
    'port'     => env('DB_PORT', 27017),
    'database' => env('DB_DATABASE', ''),
    'username' => env('DB_USERNAME', ''),
    'password' => env('DB_PASSWORD', ''),
    'options'  => array('replicaSet' => 'replicaSetName')
),

use Mongo\Mongodb\Model as Eloquent;

class User extends Eloquent {}

use Mongo\Mongodb\Model as Eloquent;

class User extends Eloquent {

    protected $collection = 'users_collection';

}

use Mongo\Mongodb\Model as Eloquent;

class MyModel extends Eloquent {

    protected $connection = 'mongodb';

}

'Moloquent'       => 'Mongo\Mongodb\Model',

class MyModel extends Moloquent {}

$users = DB::collection('users')->get();

$user = DB::collection('users')->where('name', 'John')->first();

$user = DB::connection('mongodb')->collection('users')->get();

Schema::create('users', function($collection)
{
    $collection->index('name');

    $collection->unique('email');
});

'Mongo\Mongodb\Auth\PasswordResetServiceProvider',

use Mongo\Mongodb\Model as Eloquent;

class User extends Eloquent {

    protected $connection = 'mongodb';

}

$users = User::all();

$user = User::find('517c43667db388101e00000f');

$users = User::where('votes', '>', 100)->take(10)->get();

$users = User::where('votes', '>', 100)->orWhere('name', 'John')->get();

$users = User::where('votes', '>', 100)->where('name', '=', 'John')->get();

$users = User::whereIn('age', array(16, 18, 20))->get();

$users = User::whereBetween('votes', array(1, 100))->get();

$users = User::whereNull('updated_at')->get();

$users = User::orderBy('name', 'desc')->get();

$users = User::skip(10)->take(5)->get();

$users = User::distinct()->get(array('name'));
// or
$users = User::distinct('name')->get();

$users = User::where('active', true)->distinct('name')->get();

$users = User::where('name', '=', 'John')->orWhere(function($query)
    {
        $query->where('votes', '>', 100)
              ->where('title', '<>', 'Admin');
    })
    ->get();

$users = Users::groupBy('title')->get(array('title', 'name'));

$total = Order::count();
$price = Order::max('price');
$price = Order::min('price');
$price = Order::avg('price');
$total = Order::sum('price');

$sold = Orders::where('sold', true)->sum('price');

$user = Comment::where('body', 'like', '%spam%')->get();

User::where('name', 'John Doe')->increment('age');
User::where('name', 'Jaques')->decrement('weight', 50);

$count = User->increment('age');

User::where('age', '29')->increment('age', 1, array('group' => 'thirty something'));
User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight'));

use Mongo\Mongodb\Eloquent\SoftDeletes;

class User extends Eloquent {

    use SoftDeletes;

    protected $dates = ['deleted_at'];

}

User::where('age', 'exists', true)->get();

User::where('roles', 'all', array('moderator', 'author'))->get();

User::where('tags', 'size', 3)->get();

User::where('name', 'regex', new MongoRegex("/.*doe/i"))->get();

User::where('name', 'regexp', '/.*doe/i'))->get();

User::where('name', 'not regexp', '/.*doe/i'))->get();

User::where('age', 'type', 2)->get();

User::where('age', 'mod', array(10, 0))->get();

$user = new User;
$user->name = 'John';
$user->save();

User::create(array('name' => 'John'));

$user = User::first();
$user->email = '[email protected]';
$user->save();

$user = User::first();
$user->delete();

User::destroy('517c43667db388101e00000f');

use Mongo\Mongodb\Model as Eloquent;

class User extends Eloquent {

    protected $dates = array('birthday');

}

$users = User::where('birthday', '>', new DateTime('-18 years'))->get();

use Mongo\Mongodb\Model as Eloquent;

class User extends Eloquent {

    public function items()
    {
        return $this->hasMany('Item');
    }

}

use Mongo\Mongodb\Model as Eloquent;

class Item extends Eloquent {

    public function user()
    {
        return $this->belongsTo('User');
    }

}

use Mongo\Mongodb\Model as Eloquent;

class User extends Eloquent {

    public function groups()
    {
        return $this->belongsToMany('Group', null, 'users', 'groups');
    }

}

use Mongo\Mongodb\Model as Eloquent;

class User extends Eloquent {

    public function books()
    {
        return $this->embedsMany('Book');
    }

}

$books = User::first()->books;

$user = $book->user;

$book = new Book(array('title' => 'A Game of Thrones'));

$user = User::first();

$book = $user->books()->save($book);
// or
$book = $user->books()->create(array('title' => 'A Game of Thrones'))

$book = $user->books()->first();

$book->title = 'A Game of Thrones';

$book->save();

$book = $user->books()->first();

$book->delete();
// or
$user->books()->destroy($book);

$user->books()->associate($book);

$user->save();

return $this->embedsMany('Book', 'local_key');

$books = $user->books()->where('rating', '>', 5)->orderBy('title')->get();

use Mongo\Mongodb\Model as Eloquent;

class Book extends Eloquent {

    public function author()
    {
        return $this->embedsOne('Author');
    }

}

$author = Book::first()->author;

$author = new Author(array('name' => 'John Doe'));

$book = Books::first();

$author = $book->author()->save($author);
// or
$author = $book->author()->create(array('name' => 'John Doe'));

$author = $book->author;

$author->name = 'Jane Doe';
$author->save();

$newAuthor = new Author(array('name' => 'Jane Doe'));
$book->author()->save($newAuthor);

use Mongo\Eloquent\Model as Eloquent;

class User extends Eloquent {

    protected $connection = 'mysql';

    public function messages()
    {
        return $this->hasMany('Message');
    }

}

use Mongo\Mongodb\Model as Eloquent;

class Message extends Eloquent {

    protected $connection = 'mongodb';

    public function user()
    {
        return $this->belongsTo('User');
    }

}

User::whereRaw(array('age' => array('$gt' => 30, '$lt' => 40)))->get();

// Returns a collection of User models.
$models = User::raw(function($collection)
{
    return $collection->find();
});

// Returns the original MongoCursor.
$cursor = DB::collection('users')->raw(function($collection)
{
    return $collection->find();
});

$model = User::raw()->findOne(array('age' => array('$lt' => 18)));

$client = DB::getMongoClient();
$db = DB::getMongoDB();

DB::collection('users')->timeout(-1)->get();

DB::collection('users')->where('name', 'John')
                       ->update($data, array('upsert' => true));

DB::collection('items')->project(array('tags' => array('$slice' => 1)))->get();

$limit = 25;
$projections = array('id', 'name');
DB::collection('items')->paginate($limit, $projections);

DB::collection('users')->where('name', 'John')->push('items', 'boots');
DB::collection('users')->where('name', 'John')->push('messages', array('from' => 'Jane Doe', 'message' => 'Hi John'));

DB::collection('users')->where('name', 'John')->push('items', 'boots', true);

DB::collection('users')->where('name', 'John')->pull('items', 'boots');
DB::collection('users')->where('name', 'John')->pull('messages', array('from' => 'Jane Doe', 'message' => 'Hi John'));

DB::collection('users')->where('name', 'John')->unset('note');

$user = User::where('name', 'John')->first();
$user->unset('note');

$users = User::remember(10)->get();

DB::connection()->disableQueryLog();