LoginSignup
0
0

More than 1 year has passed since last update.

Laravelのバリデーションルールで、新規登録と更新で処理を分けたい

Posted at

やりたいこと

Laravelの保存(store)、更新時(update)のメソッドで、バリデーションルールを追加すると思いますが、uniqueなどを使うときに、新規登録時と更新時で振り分けたい場合があります。
その場合に、必須チェックなど共有したいルールもあり、別のバリデーションを用意するのはちょっとなぁ...というときに使えます。

現在のルーティングによって分岐する方法

public function rules() {
    $route = $this->route()->getName();

    \Log::info($route);
}

user.storeなどが返ってきます。

フォームの入力値を取得する

ついでに、rulesの中でフォームの入力値を見たいとき

public function rules() {
    $userId = $this->user_id;
}

$thisで取れます!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0