0

I have a funny problem with Laravel 5.2. I have a chunck of text that I'm trying to display with some explodes. The problem is that Laravel is rendering a bit strange, putting some wrong ':' before the end of the foreach loop.

This is the code sample from blade template:

<ul class="list-unstyled">
  @foreach(explode('),',$items[0]->chunk) as $text)
    <li>{{ str_replace('(',' : ',str_replace(')','',$text) }}</li>
  @endforeach
</ul>

And this is the version that Laravel is rendering for it:

<ul class="list-unstyled">
  <?php foreach(explode('),',$items[0]->chunck): ?> as $text)
    <li><?php echo e(str_replace('(',' : ',$text)); ?></li>
  <?php endforeach; ?>
</ul>

I'm using NetBeans 8.0.2 as code editor (if that matters). Is there any problem with my code?

Note: If I manually edit the rendered view and move the ending PHP tag after foreach like so...

<ul class="list-unstyled">
  <?php foreach(explode('),',$items[0]->chunck) as $text): ?>
    <li><?php echo e(str_replace('(',' : ',$text)); ?></li>
  <?php endforeach; ?>
</ul>

...it works perfectly!

Edit: It seems to be a problem reading my ')' inside explode

1 Answer 1

2

Write your loop as below:-

<ul class="list-unstyled">
<?php $array = explode("),",$items[0]->chunk); ?>
  @foreach($array as $text)
    <li>{{ str_replace('(',' : ',str_replace(')','',$text) }}</li>
  @endforeach
</ul>

Hope it will help you :)

Sign up to request clarification or add additional context in comments.

1 Comment

I would personally also do the exploding in the controller and avoid logic in the view so you just iterate over the array in the view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.