PHP code example of ciayo / moloquent

1. Go to this page and download the library: Download ciayo/moloquent 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/ */

    

ciayo / moloquent example snippets


Jenssegers\Mongodb\MongodbServiceProvider::class,

$app->register(Jenssegers\Mongodb\MongodbServiceProvider::class);

$app->withEloquent();

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

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class User extends Eloquent {}

use Jenssegers\Mongodb\Eloquent\HybridRelations;

class User extends Eloquent {

    use HybridRelations;

    protected $connection = 'mysql';

}

$books = $user->books()->sortBy('title');

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

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

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

'mongodb' => [
    'driver'   => 'mongodb',
    'dsn' => env('DB_DSN'),
    'database' => env('DB_DATABASE'),
],

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class User extends Eloquent {}

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class User extends Eloquent {

    protected $collection = 'users_collection';

}

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class MyModel extends Eloquent {

    protected $connection = 'mongodb';

}

'Moloquent'       => Jenssegers\Mongodb\Eloquent\Model::class,

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');
});

Schema::create('users', function($collection)
{
    $collection->geospatial('name', '2d');
});

Schema::create('users', function($collection)
{
    $collection->geospatial('name', '2dsphere');
});

'Jenssegers\Mongodb\Auth\PasswordResetServiceProvider',

'connections' => [
    'database' => [
        'driver' => 'mongodb',
        'table'  => 'jobs',
        'queue'  => 'default',
        'expire' => 60,
    ],

'failed' => [
    'database' => 'mongodb',
    'table'    => 'failed_jobs',
    ],

Jenssegers\Mongodb\MongodbQueueServiceProvider::class,

$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', [16, 18, 20])->get();

$users = User::whereBetween('votes', [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(['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(['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');

$total = Order::max('suborder.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, ['group' => 'thirty something']);
User::where('bmi', 30)->decrement('bmi', 1, ['category' => 'overweight']);

use Jenssegers\Mongodb\Eloquent\SoftDeletes;

class User extends Eloquent {

    use SoftDeletes;

    protected $dates = ['deleted_at'];

}

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

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

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

User::where('name', 'regex', new \MongoDB\BSON\Regex("/.*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', [10, 0])->get();

$users = User::where('location', 'near', [
	'$geometry' => [
        'type' => 'Point',
	    'coordinates' => [
	        -0.1367563,
            51.5100913,
        ],
    ],
    '$maxDistance' => 50,
]);

$users = User::where('location', 'geoWithin', [
	'$geometry' => [
        'type' => 'Polygon',
	    'coordinates' => [[
            [
                -0.1450383,
                51.5069158,
            ],       
            [
                -0.1367563,
                51.5100913,
            ],       
            [
                -0.1270247,
                51.5013233,
            ],  
            [
                -0.1450383,
                51.5069158,
            ],
        ]],
    ],
]);

$locations = Location::where('location', 'geoIntersects', [
    '$geometry' => [
        'type' => 'LineString',
        'coordinates' => [
            [
                -0.144044,
                51.515215,
            ],
            [
                -0.129545,
                51.507864,
            ],
        ],
    ],
]);

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

User::create(['name' => 'John']);

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

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

User::destroy('517c43667db388101e00000f');

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class User extends Eloquent {

    protected $dates = ['birthday'];

}

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

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class User extends Eloquent {

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

}

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Item extends Eloquent {

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

}

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class User extends Eloquent {

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

}

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class User extends Eloquent {

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

}

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

$user = $book->user;

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

$user = User::first();

$book = $user->books()->save($book);
// or
$book = $user->books()->create(['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');

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Book extends Eloquent {

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

}

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

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

$book = Books::first();

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

$author = $book->author;

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

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

use Jenssegers\Mongodb\Eloquent\HybridRelations;

class User extends Eloquent {

    use HybridRelations;

    protected $connection = 'mysql';

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

}

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Message extends Eloquent {

    protected $connection = 'mongodb';

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

}

User::whereRaw(['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(['age' => array('$lt' => 18)]);

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

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

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

DB::collection('items')->project(['tags' => ['$slice' => 1]])->get();
DB::collection('items')->project(['tags' => ['$slice' => [3, 7]]])->get();

$limit = 25;
$projections = ['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', ['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', ['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();