1. Go to this page and download the library: Download convenia/laravel-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 Convenia\Mongodb\Eloquent\Model;
class Book extends Model
{
//
}
use Convenia\Mongodb\Eloquent\Model;
class Book extends Model
{
protected $collection = 'my_books_collection';
}
use Convenia\Mongodb\Eloquent\Model;
class Book extends Model
{
protected $primaryKey = 'id';
}
// MongoDB 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 Convenia\Mongodb\Eloquent\Model;
class Book extends Model
{
protected $connection = 'mongodb';
}
use Convenia\Mongodb\Auth\User as Authenticatable;
class User extends Authenticatable
{
}
use Convenia\Mongodb\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
}
use Convenia\Mongodb\Eloquent\Model;
class User extends Model
{
protected $casts = ['birthday' => 'datetime'];
}
$users = User::where(
'birthday', '>',
new DateTime('-18 years')
)->get();
use Convenia\Mongodb\Eloquent\Model;
class User extends Model
{
public function items()
{
return $this->hasMany(Item::class);
}
}
use Convenia\Mongodb\Eloquent\Model;
class Item extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
use Convenia\Mongodb\Eloquent\Model;
class User extends Model
{
public function groups()
{
return $this->belongsToMany(
Group::class, null, 'user_ids', 'group_ids'
);
}
}
use Convenia\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 Convenia\Mongodb\Eloquent\Model;
class User extends Model
{
public function books()
{
return $this->embedsMany(Book::class, 'local_key');
}
}
use Convenia\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']);
DB::beginTransaction();
User::create(['name' => 'john', 'age' => 19, 'title' => 'admin', 'email' => '[email protected]']);
// Abort the transaction, discarding any data created as part of it
DB::rollBack();
DB::beginTransaction();
User::create(['name' => 'john', 'age' => 20, 'title' => 'admin']);
// This call to start a nested transaction will raise a RuntimeException
DB::beginTransaction();
DB::collection('users')->where('name', 'john')->update(['age' => 20]);
DB::commit();
DB::rollBack();
Schema::create('users', function ($collection) {
$collection->index('name');
$collection->unique('email');
});
Schema::create('bars', function ($collection) {
$collection->geospatial('location', '2d');
});
Schema::create('bars', function ($collection) {
$collection->geospatial('location', '2dsphere');
});
use Convenia\Mongodb\Eloquent\HybridRelations;
class User extends Model
{
use HybridRelations;
protected $connection = 'mysql';
public function messages()
{
return $this->hasMany(Message::class);
}
}
use Convenia\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',
],