Git 色々

updated: 2018/6/2

 

yarn run watch (npm run watch) などを使用中にbranchの切り替えを行うと予期しないファイルが更新されてうまくmerge出来ないことがあるので出来れば一度watchを解除してからbranchを切り替える。

例)masterはsourcemapを表示させていたが、branch切替後webpackの設定をいじってsourcemapをなくしていた。masterに戻ってmergeしようとしたらsourcemapが自動で付与されてそのファイルが衝突してしまうようになる。

 

 

git ミスしたpushの取り消し 

@local

git reset --soft HEAD^

--soft: local の code は削除しないで維持

HEAD^: ひとつ前

 

git push origin master -f

-f: 強制的に push

PHP 倉庫

updated: 2018/4/6

 

複数の値を返す

>>> 配列を使う

return array($a, $b);

参考:

関数/メソッドから複数の値を返すには(多値返却) | hydroculのメモ

 

配列に連想配列を追加

$hoge = array('key1'=>'value1');
$hoge += array('key2'=>'value2');

var_dump($hoge);
//=>array(2) { ["key1"]=> string(6) "value1" ["key2"]=> string(6) "value2" }

参考:

qiita.com

 

配列 リファレンス

PHP: 配列 関数 - Manual

 

配列演算子

$a = [1,2];

$b = [1,3];

$c = [1,3,4];

$d = [];

$d += $a;

>>> $d = [1,2]

$d += $b;

>>> $d = [1,2]

$d += $c;

>>> $d = [1,2,4]

PHP: 配列演算子 - Manual

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 が出来るようになる。