Hi all!
I’m trying to @foreach a var in my blade template but it generates these errors:
Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function addLoop() on null in /srv/www/vardgivare.test/current/web/app/uploads/cache/70cbb29107ef6c211899c87a24a38b28a8e14586.php on line 16
ErrorException: Call to a member function addLoop() on null (View: /srv/www/vardgivare.test/current/web/app/themes/halland/resources/views/partials/content-single.blade.php) (View: /srv/www/vardgivare.test/current/web/app/themes/halland/resources/views/partials/content-single.blade.php) in /srv/www/vardgivare.test/current/web/app/uploads/cache/70cbb29107ef6c211899c87a24a38b28a8e14586.php on line 16
My Controller (site/web/app/themes/halland/app/theme/Comments.php):
namespace App\Theme;
class Comments
{
public function __construct()
{
}
/**
* Returns array of breadcrumbs.
* @return array
*/
public function getComments()
{
$comments = get_comments();
return $comments;
}
}
App.php (site/web/app/themes/halland/app/controllers/app.php)
public function comments()
{
$comments = new \App\Theme\Comments();
$comments = $comments->getComments();
var_dump($comments); // <- This prints out my comments just fine...
return $comments;
}
comments.blade.php (site/web/app/themes/halland/resources/views/partials/comments.blade.php)
@php
if (post_password_required()) {
return;
}
@endphp
<section id="comments" class="comments">
@if (have_comments())
<h2>
{!! sprintf(_nx('Ett svar till “%2$s”', '%1$s svar till “%2$s”', get_comments_number(), 'comments title', 'sage'), number_format_i18n(get_comments_number()), '<span>' . get_the_title() . '</span>') !!}
</h2>
@if(isset($comments))
@foreach ($comments as $comment)
<p> {{ $comment->comment_ID }} </p> <- This bit doesn't work!
@endforeach
@endif
@if (get_comment_pages_count() > 1 && get_option('page_comments'))
<nav>
<ul class="pager clearfix">
@if (get_previous_comments_link())
<li class="previous left">@php(previous_comments_link(__('← Äldre kommentarer', 'sage')))</li>
@endif
@if (get_next_comments_link())
<li class="next right">@php(next_comments_link(__('Nyare kommentarer →', 'sage')))</li>
@endif
</ul>
</nav>
@endif
@endif
@if (!comments_open() && get_comments_number() != '0' && post_type_supports(get_post_type(), 'comments'))
<div class="panel">
<div class="panel__body">
{{ __('Kommentarer är stängda för denna artikeln.', 'sage') }}
</div>
</div>
@endif
<div class="panel">
<div class="panel__body">
@php(comment_form(array(
'class_submit' => 'btn btn--primary',
'comment_field' => '<p class="comment-form-comment"><label for="comment" class="label">' . _x( 'Comment', 'noun' ) .
'</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true" class="textarea">' .
'</textarea></p>',
'logged_in_as' => '<p class="logged-in-as mb3">' .
sprintf(
__( 'Inloggad som <a href="%1$s">%2$s</a>. <a href="%3$s" title="Logga ut från detta kontot" class="btn btn--outline btn__small">Logga ut?</a>' ),
admin_url( 'profile.php' ),
$user_identity,
wp_logout_url( apply_filters( 'the_permalink', get_permalink( ) ) )) .
'</p>',
)))
</div>
</div>
</section>