Like this?
foreach ($array as $a) { echo $a; }
or depending on what you want to do you could use one of the laravel array helpers maybe? https://laravel.com/docs/5.6/helpers#arrays
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Is it possible to use foreach() on a single line? Just like we use "if and else" ?
Like this?
foreach ($array as $a) { echo $a; }
or depending on what you want to do you could use one of the laravel array helpers maybe? https://laravel.com/docs/5.6/helpers#arrays
Yes, but it doesn't work in my case:
alert()->error('ErrorMessage', foreach($validator->errors() as $error) { $error });
of course that is not working. What do you want to do exactly? Join the errors with a comma? Get the first?
I don't know what $validator->errors() returns but I assume an array with error messages?
// join all error messages with a comma
alert()->error('ErrorMessage', join(', ', $validator->errors()));
// only show the first with use of laravel helper 'array_first()'
alert()->error('ErrorMessage', array_first($validator->errors()));
I want to output all errors wrapped by a div, like so:
<div class="error">{{ $error }}</div>
$validator->errors() returns an array, yes.
Can you show the output of dd($validator->errors())?
here's DD output:
MessageBag {#516 ▼
#messages: array:2 [▼
"name" => array:1 [▶]
"price" => array:1 [▶]
]
#format: ":message"
}
try
alert()->error('ErrorMessage', '<div class="error"><div>'.implode('</div><div>', $validator->errors().'</div></div>');
it should make something like
<div class="error">
<div>error1</div>
<div>error2</div>
// etc
</div>
Throwing an error: "implode(): Invalid arguments passed"
This solution doesn't look good, is there a more elegant way to do that?
I have a working solution:
alert()->error('ErrorMessage', $validator->errors()->first());
But it shows only the frist error.
This solution doesn't look good, is there a more elegant way to do that?
Not the way you're needing the output, and wanting the code on a single line...
I'm sure you probably don't want the output to be error1, error2, error3, but want each error in it's own div wrapped in a <div class="error"> so each error is on it's own line like I showed above?
Try changing $validator->errors() to $validator->errors()->all()
I'm sure you probably don't want the output to be error1, error2, error3, but want each error in it's own div wrapped in a so each error is on it's own line?
Yes, just like a list:
try this
alert()->error('ErrorMessage', '<div class="error"><li>'.implode('</li><li>', $validator->errors()->all()).'</li></div>');
If you want them "in a list"
alert()->error('ErrorMessage', '<div class="error"><li>'.implode('</li><li>', $validator->errors()->all()).'</li></div>');
This works, bro. Thanks. Why you've addded ->all()? Isn't $validator->errors() already showing all errors?
"showing all errors"... no. It's a message bag that contains all errors yes, but it's not a normal array, which ->all() turns it into so that implode() will work with it.
From the validation docs:
Retrieving All Error Messages For All Fields
To retrieve an array of all messages for all fields, use the all method:
foreach ($errors->all() as $message) {
//
}
Also, why foreach() won't work in my case? Even if i add ->all() it throws an error.
because your output needs to be a single string to pass to alert()->error() for display.
alert()->error('ErrorMessage', foreach($validator->errors() as $error) { $error });
that's creating output for each individual error separately, so it's not a single string containing all errors. Since you're using blade syntax ({ $error }), it's using <php echo $error; ?> for each item, which won't work for what you're doing.
ok, ty again.
You're welcome!
Please or to participate in this conversation.