on3ss
on3ss

Reputation: 53

How do i pass an eloquent collection to a validation rule?

I have this rule:

Validator::make($data, [
    'category' => [
        'required',
        Rule::notIn(['news', 'article']),
    ],
]);

The problem is that news and article are stored in another table. How do i pass the the data from that table into notIn instead?

Upvotes: 0

Views: 398

Answers (1)

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

use laravel exists validation

ref link https://laravel.com/docs/8.x/validation#specifying-a-custom-column-name

Validator::make($data, [
    'category' => [
        'required',
        Rule::exists('tableName')->where(function ($query) {
            $query->where('column', "active ");
        }),
    ],
]);
``

Upvotes: 1

Related Questions