Laravel + VueJS multi vue files loading

@webpack.mix.js

mix.js('resources/assets/js/app.js', 'public/js')
.js('resources/assets/js2/app2.js', 'public/js');

 

@resources/assets/js/app.js

app.js に実装したい routing, components を読み込む
const routes = [
{ path: '/user/:id',
component: Profile,
children: [
{
path: '', component: Activity,
},

{
path: 'follow', component: Follows
},

{
path: 'activity', component: Activity
}
]
},

同一 app.js ファイル内での link は router-link で

<ul>
<li v-for="list in currentNav">
<router-link :to="list[0]" activeClass="active" v-html="list[1]" >{{ list[1] }}</router-link>
</li>
</ul>

app2.js への link は <a href="hoge"> を使ってページをリロードさせて app2.js を読み込ませる。 

 

SPAにした時のファイルの肥大化を分散して防ぐ

 

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") }

 

 

jenssegers/laravel-mongodb Basic Usage on cloud9 with php7⑤ EmbedsMany relationships test

tinker

$mongoUser = new MongoUser(['id' => 1, 'name' => 'Max']);

 

$mongoMessage = new MongoMessage(['id' => 1, 'group' => '1', 'body' => 'Something', 'sender' => 'max', 'receiver' => 'Monica']);

 

$mongoMessage = $mongoUser->mongoMessages()->save($mongoMessage)

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

mongo shell

db.mongo_users.find()

{ "_id" : ObjectId("5a992ec825dff6178d1de756"),

"id": 1,

"name" : "Max", 

"updated_at" : ISODate("2018-03-02T11:00:24Z"),

"created_at" : ISODate("2018-03-02T11:00:24Z"),

"mongoMessages" :

  [ { "group" : 1,

   "body" : "something",

   "sender" : "Max",

   "receiver" : "Monica",

   "updated_at" : ISODate("2018-03-02T12:45:23Z"),

   "created_at" : ISODate("2018-03-02T12:45:23Z"),

   "_id" : ObjectId("5a99476325dff61efa31a1e2") }

  { "group" : 2,

   "body" : "something",

   "sender" : "Tom",

   "receiver" : "Anna",

   "updated_at" : ISODate("2018-03-02T12:45:23Z"),

   "created_at" : ISODate("2018-03-02T12:45:23Z"),

   "_id" : ObjectId("5a99476325dff61efa31a1e2") }

] }

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 を経由してデータベースに値が入れられるかの違い

error: SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = c9 and table_name = migrations)

cloud9 上で php artisan migrate:reset 時に下記のエラー

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = c9 and table_name = migrations)

composer dump-autoload してもダメな時

なぜか

phpmyadmin-ctl install で phpmyadmin を立ち上げると

php artisan migrate:reset が出来るようになる。

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