Roots Discourse

Why can't I foreach my var?

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 &ldquo;%2$s&rdquo;', '%1$s svar till &ldquo;%2$s&rdquo;', 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(__('&larr; Äldre kommentarer', 'sage')))</li>
					@endif
					@if (get_next_comments_link())
						<li class="next right">@php(next_comments_link(__('Nyare kommentarer &rarr;', '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>

What do you get when you var_dump the $comments variable in your blade?

Also, which template is calling the comments.blade.php partial? If it’s not a single you’ll probably need to use a static method in your Controller and pass the post ID to get the right comments.

@php(var_dump($comments))

^ Gives me the comments in an array. As expected I’d say…

It’s called from single.blade.php I’m afraid…

Hello peeps. I finally figured out what caused the error. The blade file used to display the comments was not included by blade but included using php so I guess it lost it’s context.

1 Like

Fixed by https://github.com/roots/sage/pull/2095

1 Like