jenssegers/laravel-mongodb Basic Usage on cloud9 with php7③ 導入するメリット

updated: 2018/3/6

 

jenssegers/laravel-mongodb を導入するメリット

MySQL Relations

If you're using a hybrid MongoDB and SQL setup, you're in luck! The model will automatically return a MongoDB- or SQL-relation based on the type of the related model. Of course, if you want this functionality to work both ways, your SQL-models will need use the Jenssegers\Mongodb\Eloquent\HybridRelations trait. Note that this functionality only works for hasOne, hasMany and belongsTo relations.

Example SQL-based User model:

use Jenssegers\Mongodb\Eloquent\HybridRelations;

class User extends Eloquent {

    use HybridRelations;

    protected $connection = 'mysql';

    public function messages()
    {
        return $this->hasMany('Message');
    }

}

And the Mongodb-based Message model:

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Message extends Eloquent {

    protected $connection = 'mongodb';

    public function user()
    {
        return $this->belongsTo('User');
    }

}

EmbedsMany Relations

If you want to embed models, rather than referencing them, you can use the embedsMany relation. This relation is similar to the hasMany relation, but embeds the models inside the parent object.

REMEMBER: these relations return Eloquent collections, they don't return query builder objects!

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class User extends Eloquent {

    public function books()
    {
        return $this->embedsMany('Book');
    }

}

You access the embedded models through the dynamic property:

$books = User::first()->books;

The inverse relation is automagically available, you don't need to define this reverse relation.

$user = $book->user;

Inserting and updating embedded models works similar to the hasMany relation:

$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'])

You can update embedded models using their save method (available since release 2.0.0):

$book = $user->books()->first();

$book->title = 'A Game of Thrones';

$book->save();

You can remove an embedded model by using the destroy method on the relation, or the delete method on the model (available since release 2.0.0):

$book = $user->books()->first();

$book->delete();
// or
$user->books()->destroy($book);

If you want to add or remove an embedded model, without touching the database, you can use the associate and dissociate methods. To eventually write the changes to the database, save the parent object:

$user->books()->associate($book);

$user->save();

Like other relations, embedsMany assumes the local key of the relationship based on the model name. You can override the default local key by passing a second argument to the embedsMany method:

return $this->embedsMany('Book', 'local_key');

Embedded relations will return a Collection of embedded items instead of a query builder. Check out the available operations here: https://laravel.com/docs/master/collections

EmbedsOne Relations

The embedsOne relation is similar to the EmbedsMany relation, but only embeds a single model.

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Book extends Eloquent {

    public function author()
    {
        return $this->embedsOne('Author');
    }

}

You access the embedded models through the dynamic property:

$author = Book::first()->author;

Inserting and updating embedded models works similar to the hasOne relation:

$author = new Author(['name' => 'John Doe']);

$book = Books::first();

$author = $book->author()->save($author);
// or
$author = $book->author()->create(['name' => 'John Doe']);

You can update the embedded model using the save method (available since release 2.0.0):

$author = $book->author;

$author->name = 'Jane Doe';
$author->save();

You can replace the embedded model with a new model like this:

$newAuthor = new Author(['name' => 'Jane Doe']);
$book->author()->save($newAuthor);

参考:

github.com

 

Database Seeding

Laravel の Database Seeding を mongoDB で使えるようになる

@App/Message.php

namespace App;

use Moloquent;

 

class Message extends Moloquent {

  protected connection = 'mongodb';

  protected collection = 'messages';

}

 

@MessagesSeeder.php

class MessagesSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$message = new Message();
$message->body = "Seeded sample";
$message->user_id = 1;
$message->receiver_id = 2;
$message->read = 0;
$message->save();

$message = new Message();
$message->body = "Seeded sample2";
$message->user_id = 2;
$message->receiver_id = 1;
$message->read = 0;
$message->save();

 

デメリット

mongoDB の DBref が実装しにくい??

mongoDB 同士の ref が標準機能としてパッケージに無いので、ref 元を Mysql ベースのデータベースにして Mysql Relations で妥協

github.com

Angular builtin validator for email

import { Component, OnInit } from "@angular/core";

import { FormGroup, FormControl, Validators } from "@angular/forms";

 

@Component({
selector: 'app-signup',
templateUrl: './signup.component.html'
})

export class SignupComponent implements OnInit {
myForm: FormGroup;

ngOnInit() {
this.myForm = new FormGroup({
firstName: new FormControl(null, Validators.required),
lastName: new FormControl(null, Validators.required),
email: new FormControl(null, [
Validators.required,
Validators.pattern("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")
]),
password: new FormControl(null, Validators.required),
});
}
}

 

ref:

max Angular MEAN lecture 58

vue-awesome-swiper で swiper を二つ使用し、かつ swiper-container を入れ子にした場合の swiper 同士の干渉エラー

updated: 2018.11.14

 

サイドメニュー - メインページ ---- swiper 1

[メインページ内]  コンテンツ1 - コンテンツ2 - コンテンツ3 ---- swiper 2

 

swiper 2 が swiper 1 の中に入れ子になっている場合、swiper2 の swiper-slide-prev が swiper 1 に干渉して表示される。

 

solution:

.swiper-slide-prev {

  display: none

}

これだと vue-awesome-swiper が正常に動作しなくなる。

.swiper-slide-prev {

  margin-right: foo px;

}

これでもうまくいかない。

.swiper-slide-prev {

  visibility: hidden;

}

解決

display: none ボックス自体を無くす

visibility: hidden ボックスは無くさないが非表示にする。レイアウトが崩れない。 

 

参考:

www.htmq.com

 

related:

monteecristoo.hatenablog.com

 

swiper-container がスワイプ可能域

swiper1 > swiper-container と swiper2 > swiper-container の画面配置でスワイプのコントロールが可能になる。

 

problem:

swiper2 (メインページ) の上に swiper1 (サイドメニュー) がレイヤーしてほしいが swiper1 と swiper2 が入れ子なので z-index が使えず、clip で .swiper2 .swiper-container の表示を削って swiper-container のレイヤー構造はできたが .swiper2 > .swiper-container > swiper-slide を swiper-container の上に表示させる方法が見つからなかった。(swiper-slide ごと clip で表示が削られる)

またCSS3 では clip は非推奨。

 

サイドメニュースワイプ可能域を左側に20px取り、メインページにpadding-left: 20px を設定することで妥協

 

update:

上記妥協案を取らずに根元から変更

side-nav(スワイプ対応 left to right)は vuetify の navigation-drawer

メインページのスワイプ(right to left) は vue-hammer を使ってイベント発火でcomponent の切り替え

を使ったほうがスムーズに実装できた。 

vue-loader を v13.0.0 以降にアップデートしたら "Failed to mount component: template or render function not defined"

vue-loader を v13.0.0 以降にアップデートしたら

"Failed to mount component: template or render function not defined"

 

vue-loader@13.0.0 以降 ES module が default で true になっているため、古い CommonJS でエラーが発生

solution: 

// before
const Foo = require('./Foo.vue')

// after
const Foo = require('./Foo.vue').default

Vue.component('example', require('./components/Example.vue').default);

 

 参考:

github.com

github.com

mozjpeg コンパイリングエラー cloud9

cloud9 で Laravel mix 使用時、下記のエラーが発生

cjpeg: error while loading shared libraries: libpng16.so.16

error write epipe

 

エラーがどのタイミングで発生したか把握していない

ubuntu 特有のエラーかも

環境:

Ubuntu 14.04.5 LTS (cloud9)

laravel-mix@2.0.0

vue@2.5.7

vue-loader@14.1.1

vue-template-compiler@2.5.13

 

solution: 

sudo apt-get update

sudo apt-get install nasm

 

参考:

github.com

github.com

github.com

Atom Editor npm install --save-dev error

 

windows10

nvm 使用

node@9.4.0

npm@5.6.0

npm 使用時、npm install は出来るが npm install --save-dev で install されないエラー

 

** windows の問題かも

powershellコマンドプロンプトで npm install --save-dev ->>> error 表示無く、package.json も更新されるが実際にインストールされない

Git Bash で npm install --save-dev ->>> OK

** 但し、terminal package のデフォルト起動を Git Bash に変更しても Atom 経由でコマンドを実行するとエラーが発生する。

Error: In Database.php line 198: No suitable servers found ('serverSelectionTryOnce' set) : [connection refused calling ismaster on '127.0.0.1:27017']

updated: 2018 9/26

 

Error: In Database.php line 198: No suitable servers found ('serverSelectionTryOnce' set) : [connection refused calling ismaster on '127.0.0.1:27017']

 

mongoDB と mysql を使った multiple Database 環境で php artisan migrate をした際発生。

solution: mongoDB を起動していなかった ->>> ./mongod 

 

また cloud9 上でMYSQLが起動していなく

tinker 上で以下のコマンドでエラーが出た場合

factory(App\User::class)->create()

>>> .... SQLSTATE[HY000] [2002] Connection refused ....

 apache が寝てるかも・・・

apache を起動させるか

solution: cloud9 terminal で phpmyadmin-ctl install