1. Go to this page and download the library: Download ahmedash95/mongodb 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/ */
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, 'users', 'groups');
}
}
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');
}
}
$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');
}
}