Check if any error message exists and show all of them in laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Efficiently Displaying Laravel Error Messages: A Comprehensive Guide
Introduction: In Laravel, error handling is an essential aspect of application development. The framework offers various methods to display error messages in a structured manner. This blog post aims to provide a comprehensive guide on how to check if any error message exists and show all of them in Laravel using efficient techniques.
1. Using $errors->all() as a variable: As we saw earlier, the first approach involves storing the results from $errors->all() into a temporary variable named $something, which is then used to check existence with if(!empty($something)). This method works fine but may seem redundant considering that $errors->all() itself returns a collection of error messages. An alternative can be using the same function call directly in the condition logic as follows:
<?php
if(count($errors->all()) != 0):
?>
<div class = "alert alert-error">
@foreach ($errors->all('<p>:message</p>') as $input_error)
{{ $input_error }}
@endforeach
</div>
<?php endif; ?>
The above code uses count() to check if the collection of error messages is empty (i.e., 0 elements present) or not, and only renders the error messages if there are any. This approach eliminates the need for a temporary variable $something.
2. Directly using $errors->all() in Blade templates: Another option is to directly use the collection returned by $errors->all() inside the Blade view. Laravel provides the @error directive that checks if any error messages exist and displays them accordingly as shown below:
<?php
?>
@if($errors->any())
<div class = "alert alert-error">
@foreach ($errors->all('<p>:message</p>') as $input_error)
{{ $input_error }}
@endforeach
</div>
@endif
3. Checking error messages at the controller level: To further improve this approach, we can check for errors in the Controller and pass them to the view using a variable or session. For example, the following code snippet illustrates this technique:
<?php
namespace App\Http\Controllers;
class HomeController extends Controller {
public function show() {
$data = [];
if(count($errors->all()) == 0): // Check for zero error messages
$data['success'] = true;
return view('home', compact('data'));
else:
$data['errors'] = $errors->all();
return view('error', compact('data'));
endif;
}
}
In the above code, we check for error messages in the controller and render different views depending on whether there are any errors or not. This approach provides us with more control over how our application responds to error messages.
Conclusion: In summary, Laravel offers various ways to display error messages efficiently. Whether you use $errors->all() directly or check for their existence in the controller and render different templates based on that, both approaches can help improve your application's error handling mechanisms. For more insights into Laravel development best practices, refer to our blog posts published at https://laravelcompany.com.