Support the ongoing development of Laravel.io →
Blade Forms
Last updated by @ajax30 1 year ago.
0
moderator

For the success message it is possible by also flash the comment id to the session.

Then you can do something like:

@if (session('success') && session('success_comment_id', $comment->id))
    @include('themes/' .$theme_directory . '/partials/success')
@endif

For the error message it is maybe possible with the parent id

@if (session('error') && session('error_comment_id') === ($comment->id ?? ''))
    @include('themes/' .$theme_directory . '/partials/errors')
@endif

faisal liked this reply

1

I get an Undefined variable: comment error from comment-form.blade.php. :(

0

in blade

@foreach ($comments as $comment)
  @if (null == $comment->parent_id)
    <li class="depth-1 comment">
      <!-- Comment content -->
      ...

      // Comments form template here!
      @auth
        <div class="comment__reply">
          <a class="comment-reply-link" href="#0">Reply</a>
        </div>
      @endauth

      {{-- Comment replies --}}
      ...

      <!-- Success alert -->
      @if (session('success') && session('comment_id') == $comment->id)
        @include('themes/' . $theme_directory . '/partials/success')
      @endif

      <!-- Error alert -->
      @if (session('error') && session('comment_id') == $comment->id)
        @include('themes/' . $theme_directory . '/partials/errors')
      @endif

      <!-- Comment form -->
      <form method="post" action="{{ route('comment.submit') }}" autocomplete="off">
        ...
      </form>
    </li>
  @endif
@endforeach
<!-- Success alert -->
@if (session('success') && session('comment_id') == $comment->id)
    @include('themes/' . $theme_directory . '/partials/success')
@endif

<!-- Error alert -->
@if (session('error') && session('comment_id') == $comment->id)
    @include('themes/' . $theme_directory . '/partials/errors')
@endif
<form method="post" action="{{ route('comment.submit') }}" autocomplete="off">
    @csrf
    <fieldset>
        <!-- Store comment ID in session -->
        @if (isset($comment))
            <input type="hidden" name="comment_id" value="{{ $comment->id }}">
        @endif

        <!-- Form fields -->
        ...

        <input name="submit" id="submit" class="btn btn--primary btn-wide btn--large h-full-width" value="Add Comment" type="submit">
    </fieldset>
</form>

in controller

$comment = [
    'user_id' => Auth::user()->id,
    'article_id' => $request->get('article_id'),
    'parent_id' => $request->get('parent_id'),
    'body' => $fields['msg'],
    'approved' => 0
];

// Insert comment in the 'comments' table
$query = Comment::create($comment);

if ($query) {
    // Store the comment ID in the session
    session()->flash('comment_id', $query->id);

    return redirect()->back()->with('success', 'Your comment is pending.');
} else {
    return redirect()->back()->with('error', 'Adding comment failed');
}
Last updated 1 year ago.
0

@sasho Some of the code in the answer is not formatted and is hard to read it :)

Last updated 1 year ago.
0
@foreach ($comments as $comment)
  @if (null == $comment->parent_id)
    <li class="depth-1 comment">
      <!-- Comment content -->
      ...

      // Comments form template here!
      @auth
        <div class="comment__reply">
          <a class="comment-reply-link" href="#0">Reply</a>
        </div>
      @endauth

      {{-- Comment replies --}}
      ...

      <!-- Success alert -->
      @if (session('success') && session('comment_id') == $comment->id)
        @include('themes/' . $theme_directory . '/partials/success')
      @endif

      <!-- Error alert -->
      @if (session('error') && session('comment_id') == $comment->id)
        @include('themes/' . $theme_directory . '/partials/errors')
      @endif

      <!-- Comment form -->
      <form method="post" action="{{ route('comment.submit') }}" autocomplete="off">
        ...
      </form>
    </li>
  @endif
@endforeach
Last updated 1 year ago.
0
<!-- Success alert -->
@if (session('success') && session('comment_id') == $comment->id)
    @include('themes/' . $theme_directory . '/partials/success')
@endif

<!-- Error alert -->
@if (session('error') && session('comment_id') == $comment->id)
    @include('themes/' . $theme_directory . '/partials/errors')
@endif
Last updated 1 year ago.
0
<form method="post" action="{{ route('comment.submit') }}" autocomplete="off">
    @csrf
    <fieldset>
        <!-- Store comment ID in session -->
        @if (isset($comment))
            <input type="hidden" name="comment_id" value="{{ $comment->id }}">
        @endif

        <!-- Form fields -->
        ...

        <input name="submit" id="submit" class="btn btn--primary btn-wide btn--large h-full-width" value="Add Comment" type="submit">
    </fieldset>
</form>
Last updated 1 year ago.
0
$comment = [
    'user_id' => Auth::user()->id,
    'article_id' => $request->get('article_id'),
    'parent_id' => $request->get('parent_id'),
    'body' => $fields['msg'],
    'approved' => 0
];

// Insert comment in the 'comments' table
$query = Comment::create($comment);

if ($query) {
    // Store the comment ID in the session
    session()->flash('comment_id', $query->id);

    return redirect()->back()->with('success', 'Your comment is pending.');
} else {
    return redirect()->back()->with('error', 'Adding comment failed');
}
Last updated 1 year ago.
0

@ajax30 As you see even is separate messages the code is not formatted correctly, but i assume you get the idea. Cheers

0
moderator

@ajax30 @sasho I have updated the messages to get the formatting as wanted :)

(You need to have 3 ` to get a code block)

sasho liked this reply

1

@sasho I get an Undefined variable: comment (View: resources\views\themes\calvin\partials\comment-form.blade.php) error when submitting a reply.

0

@ajax30 Pass the variable to the view ‘comments’ => $comments

0

@sasho I do that already in the show() method:

public function show( $slug ) {
		// Single article
		$article     = Article::firstWhere( 'slug', $slug );
		$old_article = Article::where( 'id', '<', $article->id )->orderBy( 'id', 'DESC' )->first();
		$new_article = Article::where( 'id', '>', $article->id )->orderBy( 'id', 'ASC' )->first();

		// Comments
		$commentsQuery  = $this->get_commentQuery( $article->id );
		$comments_count = $commentsQuery->count();

		// If infinite scroll, paginate comments (to be loaded one page per scroll),
    // Else show them all 

		if (boolval($this->is_infinitescroll)) {
			$comments = $commentsQuery->paginate($this->comments_per_page);
		} else {
			$comments = $commentsQuery->get();
		}

		return view( 'themes/' . $this->theme_directory . '/templates/single',
		             array_merge( $this->data, [
			             'categories'        => $this->article_categories,
			             'article'           => $article,
			             'old_article'       => $old_article,
			             'new_article'       => $new_article,
			             'comments'          => $comments, // pass comments to the view
			             'comments_count'    => $comments_count,
                         'comments_per_page' => $this->comments_per_page,
			             'tagline'           => $article->title,
			             'is_infinitescroll' => $this->is_infinitescroll
		             ] )
		);
}
Last updated 1 year ago.
0

Do dd(); on comments before passing them in view and work your way back, somewhere there is an issue. Also do php artisan optimize and clear view cache

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Razvan ajax30 Joined 2 Oct 2021

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.