HOW TO CREATE A CUSTOM ERROR MESSAGE IN LARAVEL

HOW TO CREATE A CUSTOM ERROR MESSAGE:

Laravel has Built-in error messages if a validation fails, however we might need to alter those messages to make our application exceptional. This formula demonstrates a couple of various approaches to make custom mistake messages.
how to create custom error message in laravel
how to create custom error message in laravel

Follow these steps to create a custom error message:

1.  Create a route in routes.php to hold the form:
Route::get('myform', function()
{
return View::make('myform');
});
2.  Create a view named myform.php and add a form:
<h1>User Info</h1>
<?php $messages = $errors->all
('<p style="color:red">:message</p>') ?>
<?php
foreach ($messages as $msg)
{
echo $msg;
}
?>
<?= Form::open() ?>
<?= Form::label('email', 'Email') ?>
<?= Form::text('email', Input::old('email')) ?>
<br>
<?= Form::label('username', 'Username') ?>
<?= Form::text('username', Input::old('username')) ?>
<br>
<?= Form::label('password', 'Password') ?>
<?= Form::password('password') ?>
<br>
<?= Form::submit('Send it!') ?>
<?= Form::close() ?>
3.  Create a route that handles our POST data and validates it:
Route::post('myform', array('before' => 'csrf', function()
{
$rules = array(
'email' => 'required|email|min:6',
'username' => 'required|min:6',
'password' => 'required'
);
$messages = array(
'min' => 'Way too short! The :attribute must be at
least :min characters in length.',
'username.required' => 'We really, really need a
Username.'
);
$validation = Validator::make(Input::all(), $rules,
$messages);
if ($validation->fails())
{
return Redirect::to('myform')->withErrors
($validation)->withInput();
}
return Redirect::to('myresults')->withInput();
}));
4.  Open the file app/lang/en/validation.php, where en is the default language
of the app. In our case, we're using English. At the bottom of the file, update the
attributes array as the following:
'attributes' => array(
'password' => 'Super Secret Password (shhhh!)'
),
5.  Create a route to handle a successful form submission:
Route::get('myresults', function()
{
return dd(Input::old());

});

Comments