close

How can I manually return or throw a validation error/exception in Laravel?

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error How can I manually return or throw a validation error/exception in Laravel? in php. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How can I manually return or throw a validation error/exception in Laravel Error Occurs?

Today I get the following error How can I manually return or throw a validation error/exception in Laravel? in php.

How To Solve can I manually return or throw a validation error/exception in Laravel Error ?

  1. How To Solve can I manually return or throw a validation error/exception in Laravel Error ?

    To Solve can I manually return or throw a validation error/exception in Laravel Error For Laravel 5.8:
    .
    The easiest way to throw an exception is like this:
    throw new \ErrorException('Error found');

  2. How can I manually return or throw a validation error/exception in Laravel?

    To Solve can I manually return or throw a validation error/exception in Laravel Error For Laravel 5.8:
    .
    The easiest way to throw an exception is like this:
    throw new \ErrorException('Error found');

Solution 1

Laravel <= 6.2 this solution worked for me:

$validator = Validator::make([], []); // Empty data and rules fields
$validator->errors()->add('fieldName', 'This is the error message');
throw new ValidationException($validator);

Solution 2

For Laravel 5.8:

.

The easiest way to throw an exception is like this:

throw new \ErrorException('Error found');

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment