26

In laravel for showing all error messages at once i use the following code in the view

<?php 
 $something = $errors->all(); 
 if(!empty($something)): 
?>

<div class = "alert alert-error">                      
  @foreach ($errors->all('<p>:message</p>') as $input_error)
    {{ $input_error }}
  @endforeach 
</div> 

<?php endif; ?>

But when I want to use $errors->all() instead of $something in the if condition it's showing an error

Can't use method return value in write context

Although the above code works fine, I think there may be a better ways to check if any error message exists and if it does then display it.

Nabil
  • 317
  • 10
  • 27
Suman Ghosh
  • 271
  • 1
  • 5
  • 6

4 Answers4

36

Yes, because you can't use any method as empty function parameter. From php docs:

empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.

What class is $errors? If it's your own class you can implement such method like 'isEmpty()' and then use in if statement:

if ($errors->isEmpty()) { ...
Epoc
  • 7,208
  • 8
  • 62
  • 66
Cyprian
  • 11,174
  • 1
  • 48
  • 45
  • Thanks!! I was not aware of that! – Suman Ghosh Sep 14 '12 at 09:31
  • Actually I have looked into the [documentation](https://laravel.com/api/7.x/Illuminate/Support/MessageBag.html#method_isEmpty) and there is already `$errors->isEmpty()` function implemented :) – Gucu112 Aug 06 '20 at 19:57
27

In my controller I use the following code to pass validation errors to my view:

return Redirect::to('page')
    ->withErrors($validator);

Then, in my view, I can use the following code to check if errors exist:

@if($errors->any())
<div id="error-box">
    <!-- Display errors here -->
</div>
@endif

You can also use if($errors->all()).

From the Laravel (v4) docs:

Note that when validation fails, we pass the Validator instance to the Redirect using the withErrors method. This method will flash the error messages to the session so that they are available on the next request... [A]n $errors variable will always be available in all of your views, on every request, allowing you to conveniently assume the $errors variable is always defined and can be safely used.

chipit24
  • 6,509
  • 7
  • 47
  • 67
1

Best way in laravel is below code

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
0
 <!-- /resources/views/post/create.blade.php -->

 <h2>Create Post</h2>
 @if ($errors->any())
 <div class="alert alert-danger">
 <ul>
 @foreach ($errors->all() as $error)
 <li>{{ $error }}</li>
 @endforeach
 </ul>
</div>
@endif
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 14 '23 at 01:00