3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel5.5 の 'email' バリデーションについて ( unique 指定且つ自身を除外する )

Last updated at Posted at 2018-06-06
Page 1 of 2

よくでてくるところとして、以下のものがあります。ドキュメントもこれですが、自分の環境では動かなかったです...
uniqueの部分は動いてくれるんですが、プロフィールなんかを更新するときに、自身もはじいちゃうんですよね...


use Illuminate\Validation\Rule;

Validator::make($data, [
    'email' => [
        'required',
        Rule::unique('users')->ignore($user->id),
    ],
]);

https://blog.capilano-fw.com/?p=341#unique
を参考にしつつ、以下で試してみました。一応動いています。


use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Auth;

public function rules()
    {
        $user_id = Auth::user()->id;
        return [
            //バリデーションルール
            'email' => Rule::unique('users')->ignore($user_id),
        ];
    }

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?