Place error message in Laravel 5.6 view

Asked

Viewed 2,034 times

0

Good night, you guys. I’m having trouble returning a friendly error message to the user if he tries to register an employee with an email that is already saved in the database, since in my table this attribute was "set" as Unic.

Can anyone tell me how to customize this on Laravel 5.6? Currently is returning the default error message that is this one:

Illuminate Database Queryexception (23000)

SQLSTATE[23000]: Integrity Constraint Violation: 1062 Duplicate entry '[email protected]' for key 'funcionarios_email_funcionario_unique'

I would like to put a message in the view itself for the user, like this:

"Email already registered in the database!"

The way of exception is this one:

..vendorLaravel framework src Illuminate Database Connection.php

And that’s the exception treatment:

 protected function runQueryCallback($query, $bindings, Closure $callback)
    {
        // To execute the statement, we'll simply call the callback, which will actually
        // run the SQL against the PDO connection. Then we can calculate the time it
        // took to execute and log the query SQL, bindings and time in our memory.
        try {
            $result = $callback($query, $bindings);
        }

        // If an exception occurs when attempting to run a query, we'll format the error
        // message to include the bindings with SQL, which will make this exception a
        // lot more helpful to the developer instead of just the database's errors.
        catch (Exception $e) {

            throw new QueryException(
                $query, $this->prepareBindings($bindings), $e
            );
        }

        return $result;
    }
  • you are not using the Redeemable Validate?

  • No. The validate would be a Laravel Feature?

  • Yes, you define which form fields you want to validate and which type of validation you will use. the following doc: https://laravel.com/docs/5.7/validation#Quick-writing-the-validation-Logic

1 answer

3


Beast, the ideal is for you to do a validation before persisting the data.

In Laravel it’s very simple, you can create a custom request or validate in the controller itself.

https://laravel.com/docs/5.6/validation#Rule-Unique

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $validator = Validator::make($request->all(), [
        'email' => 'required|email|unique:funcionario,email',
    ]);

    if ($validator->fails()) {
        return redirect('funcionario/create')
                        ->withErrors($validator)
                        ->withInput();
    }

    // continua e persiste os dados
    Funcionario::create($request->all());

    return redirect('funcionario/create')
    ->with('mensagem', 'Funcionário salvo com sucesso');
}
  • Thanks friend, in this your sample code would be directly in the correct Controller? The error message I could specify inside the withInput()?

  • Yes, it would be within the controller this example. Laravel himself will be in charge of producing an error message when you are using Validator. These messages are in /Resources/lang/xx/validation.php

  • If you want the standard Laravel validation messages to be in en, I would suggest this repository.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.