monteecristoo’s blog

https://twitter.com/irakaarozo

jenssegers/laravel-mongodb Basic Usage on cloud9 with php7④

App/MongoUser

namespace App;

use Moloquent;

use MongoMessage;

class MongoUser extends Moloquent
{
protected $connection = 'mongodb';
protected $collection = 'mongoUsers';

public function mongoMessages()
{
return $this->embedsMany('MongoMessage');
}
}

 

MongoUser migration file

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMongoUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::connection('mongodb')->create('mongoUsers', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('name');
$table->string('sex');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::connection('mongodb')->drop('mongoUsers');
}
}

 

tinker cosole 上

$mongoUser = new MongoUser(['body' => 'test']);

 ただし、App/MongoUser に

protected $fillable = ['body']; を加えると

$mongoUser = new MongoUser(['body' => 'test']);

 

$mongoUser = new MongoUser();

$mongoUser->body = 'test';

$mongoUser->save();

** protected $fillable = ['body']; が無くても実効可

 

恐らく

jenssegers/laravel-mongodb を経由するか Laravel 純正のEloquent を経由してデータベースに値が入れられるかの違い