[Solved] Laravel withValidator() not working as expected


I’m an idiot and didn’t read the full doc.

It needs to specify with an after function,like this:

public function withValidator($factory) {
        $result = User::where('name', $this->name)->get();
        $factory->after(function ($factory) use ($result) {
            if (!$result->isEmpty()) {
                $factory->errors()->add('User', 'Something wrong with this guy');
            }
        });
        return $factory;
}

1

solved Laravel withValidator() not working as expected