1. Go to this page and download the library: Download admnio/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 Illuminate\Foundation\Testing\DatabaseMigrations;
use DatabaseMigrations;
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'options' => [
// here you can pass more settings to the Mongo Driver Manager
// see https://www.php.net/manual/en/mongodb-driver-manager.construct.php under "Uri Options" for a list of complete parameters that you can use
'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'), //
use Jenssegers\Mongodb\Eloquent\Model;
class Book extends Model
{
//
}
use Jenssegers\Mongodb\Eloquent\Model;
class Book extends Model
{
protected $collection = 'my_books_collection';
}
use Jenssegers\Mongodb\Eloquent\Model;
class Book extends Model
{
protected $primaryKey = 'id';
}
// Mongo will also create _id, but the 'id' property will be used for primary key actions like find().
Book::create(['id' => 1, 'title' => 'The Fault in Our Stars']);
use Jenssegers\Mongodb\Eloquent\Model;
class Book extends Model
{
protected $connection = 'mongodb';
}
use Jenssegers\Mongodb\Auth\User as Authenticatable;
class User extends Authenticatable
{
}
use Jenssegers\Mongodb\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
}
use Jenssegers\Mongodb\Eloquent\Model;
class User extends Model
{
protected $dates = ['birthday'];
}
$users = User::where(
'birthday', '>',
new DateTime('-18 years')
)->get();
use Jenssegers\Mongodb\Eloquent\Model;
class User extends Model
{
public function items()
{
return $this->hasMany(Item::class);
}
}
use Jenssegers\Mongodb\Eloquent\Model;
class Item extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
use Jenssegers\Mongodb\Eloquent\Model;
class User extends Model
{
public function groups()
{
return $this->belongsToMany(
Group::class, null, 'user_ids', 'group_ids'
);
}
}
use Jenssegers\Mongodb\Eloquent\Model;
class User extends Model
{
public function books()
{
return $this->embedsMany(Book::class);
}
}
$user = User::first();
foreach ($user->books as $book) {
//
}
$book = Book::first();
$user = $book->user;
$book = $user->books()->save(
new Book(['title' => 'A Game of Thrones'])
);
// or
$book =
$user->books()
->create(['title' => 'A Game of Thrones']);
$book = $user->books()->first();
$book->title = 'A Game of Thrones';
$book->save();
$book->delete();
// Similar operation
$user->books()->destroy($book);
$user->books()->associate($book);
$user->save();
use Jenssegers\Mongodb\Eloquent\Model;
class User extends Model
{
public function books()
{
return $this->embedsMany(Book::class, 'local_key');
}
}
use Jenssegers\Mongodb\Eloquent\Model;
class Book extends Model
{
public function author()
{
return $this->embedsOne(Author::class);
}
}
$book = Book::first();
$author = $book->author;
$author = $book->author()->save(
new Author(['name' => 'John Doe'])
);
// Similar
$author =
$book->author()
->create(['name' => 'John Doe']);
Schema::create('bars', function ($collection) {
$collection->geospatial('location', '2d');
});
Schema::create('bars', function ($collection) {
$collection->geospatial('location', '2dsphere');
});
use Jenssegers\Mongodb\Eloquent\HybridRelations;
class User extends Model
{
use HybridRelations;
protected $connection = 'mysql';
public function messages()
{
return $this->hasMany(Message::class);
}
}
use Jenssegers\Mongodb\Eloquent\Model;
class Message extends Model
{
protected $connection = 'mongodb';
public function user()
{
return $this->belongsTo(User::class);
}
}
'connections' => [
'database' => [
'driver' => 'mongodb',
// You can also specify your jobs specific database created on config/database.php
'connection' => 'mongodb-job',
'table' => 'jobs',
'queue' => 'default',
'expire' => 60,
],
],
'failed' => [
'driver' => 'mongodb',
// You can also specify your jobs specific database created on config/database.php
'database' => 'mongodb-job',
'table' => 'failed_jobs',
],