monteecristoo’s blog

https://twitter.com/irakaarozo

jenssegers/laravel-mongodb Basic Usage on cloud9 with php7⑥ Mysql relationships test

App\User >>>> Mysql based Database

use Jenssegers\Mongodb\Eloquent\HybridRelations;

 

use App\MongoMessage;

 

class User extends Authenticatable

{

  use HybridRelations;

  use MongoMessage;

 

  public function mongoMessages()

  {

    return $this->hasMany(MongoMessage::class);

  }

}

 

App\MongoMessage >>>> mongoDB based Database

use Moloquent;

use App\User;

 

class MongoMessage extends Moloquent

{

  protected $connection = 'mongodb';

  protected $collection = 'mongo_messages';

  protected $fillable = ['body'];

 

  public function users()

  {

    return $this->belogsTo(User::class);

  }

}

 

on tinker

$user = App\User::first();

$user->mongoMessages()->create(['body' => 'test']);

 

on mongo shell

db.mongo_messages.find()

{ "_id" : ObjectId("5a99836325dff62b0f31a1e3"), "body" : "test", "user_id" : 1, "updated_at" : ISODate("2018-03-02T17:01:23Z"), "created_at" : ISODate("2018-03-02T17:01:23Z") }