0

I am trying to convert this PHP code:

    <?php
                    foreach ($arr as $v) {
                    echo '<tr><td>' . $v['bookTitle'] . '</td><td>';
                            $ar = array();
                            foreach ($v['authors'] as $key => $value) {
                            **$ar[] = '<a href="all?authorID=' . $key . '">' . $value . '</a>';**
                            }
                            echo implode(' , ', $ar) . '</td></tr>';
                    }

                    ?>

into Laravel code, but have problems

           @foreach ($arr as $v)
            <tr><td> {{ $v['bookTitle'] }}</td><td>
                <?php $ar = array(); ?>
                @foreach ($v['authors'] as $key => $value)
                    ***$ar[] = <a href="all?authorID=' . $key . '"> . $value . </a>;*** //{{ Html::link("all?authorID=  $key", "$value")}}

                 @endforeach
                {{implode(' , ', $ar)}}</td></tr>
             @endforeach

Can someone please help me with this?

 @foreach ($arr as $v)
                <tr><td> {{ $v['bookTitle'] }}</td><td>
                    @php $ar = array(); @endphp
                    @foreach ($v['authors'] as $key => $value)
                        @php  $ar[]; @endphp =  {{ Html::link("all?authorID=  $key", "$value")}}
                     @endforeach
                    {{implode(' , ', $ar)}}</td></tr>
                 @endforeach

FatalErrorException Cannot use [] for reading

7
  • Could you post your full code, including the value of the arrays? Also, what's not working? Commented Jul 2, 2017 at 23:18
  • This is actually a table of books and authors. You can find all the books related to an author as well as co-authors by simply clicking on the author's name. I can't convert this $ar[] = '<a href="all?authorID=' . $key . '">' . $value . '</a>'; into Laravel so that it works within @foreach Commented Jul 2, 2017 at 23:30
  • So I want to implement a $variable into the @foreach loop without echoing it, just prepare it so that I can echo it after implode() Commented Jul 2, 2017 at 23:53
  • @cyberspacelogin what version of Laravel are you using? If you're on the latest then you could use the @php [..] @endphp and not rewrite it using blade until you re-factor to process the authors within your controller? Commented Jul 3, 2017 at 10:29
  • 5.4.27 Do you mean like this:(see above) Commented Jul 3, 2017 at 10:48

2 Answers 2

1

Your mistake is at this line. The $val is not exist as your are make each object in loop to $value variable

$ar[] = <a href="all?authorID=' . $key . '"> . $val . </a>;

Secondly, the blade syntax is like below. Refer to Docs for detail usage.

 @foreach ($v['authors'] as $key => $value)
     <td><a href="{{ url('all?authorID=' . $key) }}">{{ $value }}</a></td>

 @endforeach

Note: Please keep the long code in controller instead of routes/web.php

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

4 Comments

Thank you and you are right about the long code which should be in the controller, will have this fixed. But as you can see in my first comment above I already know on which line the mistake is, what I dont know is how to transform the working php raw code into laravel form- I want to implement an array key in the loop, so that I can fill the empty array and then emplode "," inbetween all keys.
Please disregard $val doesn't exist, this was a tippo, it's $value and I have also corrected it above
I dont think need to use array to store html and implode it. better to use the way the blade is design, even plain php also do the same thing to echo variable or value in the middle of html
As I am echoing authors, I dont want the last author to be followed by a ","too, just the previous ones. That is also the reason I dont use simple html in the loop. Probably I may have to use Eloquent fil() method, I am checking how :)
0

As you're using Larvel 5.4.x, you can simply do the following and not have to re-write it using blade:

@php
    foreach ($arr as $v) {
        echo '<tr><td>' . $v['bookTitle'] . '</td><td>';
        $ar = array();
        foreach ($v['authors'] as $key => $value) {
            $ar[] = '<a href="all?authorID=' . $key . '">' . $value . '</a>';
        }
        echo implode(' , ', $ar) . '</td></tr>';
    }
@endphp

Albeit it's not the best solution, but, until you can move this logic into the controller and not have it within the view, it might be a good interim solution.

If you open up the following link for Control Structures within the Laravel documentation: https://laravel.com/docs/5.4/blade#control-structures and scroll down to the heading named "PHP" it should give you everything you need.

1 Comment

Thank you, if you say so, then I will leave it that way(will add only htmlspecialchars()), for now. Would like to also ask you why is this possible because of Larvel 5.4.x?

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.