Foreach on a single line

Published 1 month ago by AlexanderKim

Is it possible to use foreach() on a single line? Just like we use "if and else" ?

Best Answer (As Selected By AlexanderKim)
Cronix

try this

alert()->error('ErrorMessage', '<div class="error"><li>'.implode('</li><li>', $validator->errors()->all()).'</li></div>');

If you want them "in a list"

click
click
1 month ago (59,590 XP)

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

AlexanderKim

Yes, but it doesn't work in my case:

alert()->error('ErrorMessage', foreach($validator->errors() as $error) { $error });
click
click
1 month ago (59,590 XP)

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()));
AlexanderKim

I want to output all errors wrapped by a div, like so:

<div class="error">{{ $error }}</div>

$validator->errors() returns an array, yes.

Cronix
Cronix
1 month ago (571,710 XP)

Can you show the output of dd($validator->errors())?

AlexanderKim

here's DD output:

MessageBag {#516 ▼
  #messages: array:2 [▼
    "name" => array:1 [▶]
    "price" => array:1 [▶]
  ]
  #format: ":message"
}
Cronix
Cronix
1 month ago (571,710 XP)

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>
AlexanderKim

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.

Cronix
Cronix
1 month ago (571,710 XP)

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()

AlexanderKim

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:

  • First error
  • Second error
  • Third error
Cronix
Cronix
1 month ago (571,710 XP)

try this

alert()->error('ErrorMessage', '<div class="error"><li>'.implode('</li><li>', $validator->errors()->all()).'</li></div>');

If you want them "in a list"

AlexanderKim

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?

Cronix
Cronix
1 month ago (571,710 XP)

"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) {
    //
}
AlexanderKim

Also, why foreach() won't work in my case? Even if i add ->all() it throws an error.

Cronix
Cronix
1 month ago (571,710 XP)

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.

Please sign in or create an account to participate in this conversation.