Reputation: 13
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
Upvotes: 0
Views: 113
Reputation: 532
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.
Upvotes: 0
Reputation: 3538
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
Upvotes: 1