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
I get an Undefined variable: comment
error from comment-form.blade.php
. :(
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');
}
@sasho Some of the code in the answer is not formatted and is hard to read it :)
@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>
$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');
}
@ajax30 As you see even is separate messages the code is not formatted correctly, but i assume you get the idea. Cheers
sasho liked this reply
@sasho I get an Undefined variable: comment (View: resources\views\themes\calvin\partials\comment-form.blade.php)
error when submitting a reply.
@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
] )
);
}
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community