LoginSignup
0
0

laravel existsバリデーションで論理削除もチェックする

Last updated at Posted at 2023-12-16

概要

  • existsバリデーションで論理削除されている場合バリデーションエラーにする方法をまとめる。

困りごと

  • laravelのバリデーションルールにexistsという物がある。リクエストで送られた値がテーブルに存在するかをチェックできる。
  • idなどがpostされたときに使うバリデーションルールとして一般的だが、exists:テーブル名,カラム名としただけだと論理削除レコードを考慮してくれない。(論理削除されていたとしてもレコードが存在していたらバリデーションで弾かない。)

方法

  • 下記のようにテーブル名、カラム名に追加してdeleted_atとnullを追加する。

    return [
        'id' => ['exists:テーブル名,カラム名,deleted_at,null'],
    ]
    
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