Laravel: migration file を削除してしまい php artisan migrate:reset でエラー

環境:

Laravel@9.52.4

 

migrate 済みの migration ファイルを削除してしまい、php artisan migrate:reset で Migration not found とエラーが出てしまった場合。

 

solution)

php artisan migrate:fresh

>>> DB全てのテーブルが削除された後、migrate する処理

 

ref:

stackoverflow.com

Laravel: Cannot trigger ORM model event static::restored

環境:

laravel@8.83.23

 

事象:

Model を soft-delete し、その後 restore 時に特定の処理をしたいが処理が実行されない。

 

cannot works:

@models/User.php

protected static fucntion boot() {

  parent::boot();

  static::deleted(function ($user) {

    $user->profileDetail()->delete();  // works fine

  })

  static::updated(function ($user) {

    $user->name = "updated";

    $user->save();  // works fine

  })

  static::restored(function ($user) {

    $user->profileDetail()->restore();  // cannot works ????

  })

}

 

原因:

User を restore するときに 一意の model を指定していなかったため

 

User::where('id', $request->id)->withTrashed()->restore(); // requested user will be restore but cannot trigger static::restored(function ($user))

 

solution)

User::where('id', $request->id)->withTrashed()->first()->restore();

Laravel + MongoDB: error: Indirect modification of overloaded property App\....

環境:

laravel@8.83.23

jensseger/laravel-mongodb@3.8.0

 

Model Structure

Profile: {

  roles: [’admin’]

}

$profile = App\Profile::find(1);

$profile->roles = array_push($profile->roles, 'superAdmin')

 

>>> error: Indirect modification of overloaded property App\Profile

 

solution)

値を編集するのではなく、新たな値を定義する

$roles = array_values($profile->roles)

array_push($roles, 'superAdmin')

$profile->roles = $roles

$profile->save()

VueJS: handling edge case - http request で得た値をprops として子component にパスした時、mounted で値を加工しようとしたらprops の取得タイミングの都合で正しい値を取得できない。さらにその値を編集したい場合

http request で得た値をprops として子component にパスした時、mounted で値を加工しようとしたらprops の取得タイミングの都合で正しいprops の値を取得しないで処理されてしまうエラーの回避。さらにその値を編集したい場合。

 

環境:

vue@2.6.12

 

@親コンポーネント

axios.get(****).then((res) => {

  this.forChildComponent = res.data

})

 

<some-childComponent :forChildComponent=forChildComponent>

 

 

 

@子コンポーネント

props: ['forChildComponent']

 

created や mounted で値に操作しようとすると http request のレスポンスによって値が正しく取得できるか左右する

 

** ダメなケース

mounted() {

  this.targetValue = this.forChildComponent + 1

}

>>>> http request のレスポンスが遅いと undefined + 1 になってしまう

 

 

** Need fix

computed: {
  targetValue() {
    return this.forChildComponent + 1

  }

}

>>> 値は正しく取得できる。targetValue に追加処理が発生しないのならこれでOK

 

 

** Solution さらにforChildComponent を編集する必要がある場合

data() {

  return {

    newValue: ''

  }

},

computed: {

  targetValue() {
    if(newValue) {

      return newValue

    } else {

      return this.forChildComponent + 1

    }

  }

},

methods: {

  changeTargetValue() {
    this.newValue = 9

  }

}

 

直接 props を編集しようとすると

 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders.

とエラーが出るので新たに newValue を設定してエラーを回避