Learn or Die

生涯勉強。Macです。

DBの値でバリデーションチェックする

exists:テーブル名,カラム名

フィールドの値が、指定されたデータベーステーブルに存在することをバリデートする。

基本的な使用方法

<?php

'id' => 'exists:customer,id'

columnオプションを指定しない場合、フィールド名が利用される。

<?php

'id' => 'exists:customer'

テーブル名を直接指定する代わりに、Eloquentモデルを指定することもできる。

<?php

'id' => 'exists:App\Models\customer,id'

バリデーションルールのクエリをカスタマイズしたい

<?php

'id' => [
            'required',
            Rule::exists('customer')->where(function ($query) {
                $query->where('del_flg', 0);
            }),
        ],

参考サイト

https://readouble.com/laravel/7.x/ja/validation.html