如何防止在这个Laravel应用中成功提示的重复出现?

huangapple go评论57阅读模式
英文:

How do I prevent the repetition of a success alert in this Laravel app?

问题

I have made a blogging application in Laravel 8.

I am working on the functionality of adding comment replies.

In the controller, I add comments like this:

$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) {
  return redirect()->back()->with('success', 'Your comment is pending.');
} else {
  return redirect()->back()->with('error', 'Adding comment failed');
}

I have added the comments form under each comment:

@foreach ($comments as $comment)
  @if (null == $comment->parent_id)
    <li class="depth-1 comment">
      <div class="comment__avatar">
          <img class="avatar" src="{{ asset('images/avatars/' . $comment->user->avatar) }}" alt="{{ $comment->user->first_name }} {{ $comment->user->last_name }}" width="50" height="50">
      </div>
      <div class="comment__content">
          <div class="comment__info">
              <div class="comment__author">{{ $comment->user->first_name }} {{ $comment->user->last_name }}</div>
              <div class="comment__meta">
                  <div class="comment__time">{{ date('jS M Y', strtotime($comment->created_at)) }}</div>
                  @auth
                    <div class="comment__reply">
                      <a class="comment-reply-link" href="#0">Reply</a>
                    </div>
                  @endauth
              </div>
          </div>
          <div class="comment__text">
              <p>{{ $comment->body }}</p>
          </div>
      </div>

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

      {{-- Comment replies --}}
      @if (count($comment->replies))
        <ul class="children">
          @foreach ($comment->replies as $reply)
            <li class="depth-2 comment">
              <div class="comment__avatar">
                <img class="avatar" src="{{ asset('images/avatars/' . $reply->user->avatar) }}" alt="" width="50" height="50">
              </div>
              <div class="comment__content">
                  <div class="comment__info">
                      <div class="comment__author">{{ $reply->user->first_name }} {{ $reply->user->last_name }}</div>
                      <div class="comment__meta">
                          <div class="comment__time">{{ date('jS M Y', strtotime($reply->created_at)) }}</div>
                      </div>
                  </div>
                  <div class="comment__text">
                    <p>{{ $reply->body }}</p>
                  </div>
              </div>
            </li>
          @endforeach
        </ul>
      @endif
    </li>
  @endif
@endforeach

The comments form (partials\comment-form.blade.php):

@if (session('success'))
    @include('themes/' .$theme_directory . '/partials/success')
@endif

@if (session('error'))
    @include('themes/' .$theme_directory . '/partials/errors')
@endif

<form method="post" action="{{ route('comment.submit') }}" autocomplete="off">
  @csrf
    <fieldset>
        <input type="hidden" name="article_id" value="{{ $article->id }}">
        <input type="hidden" name="parent_id" value="{{ $comment->id ?? '' }}">

        <div class="message form-field">
            <textarea name="msg" id="message" class="h-full-width" placeholder="Your Message"></textarea>

            @error('msg')
            <p class="help-block text-danger">{{ $message }}</p>
            @enderror
        </div>
        <br>
        <input name="submit" id="submit" class="btn btn--primary btn-wide btn--large h-full-width" value="Add Comment" type="submit">
    </fieldset>
</form>

The problem

Due to the fact that the comments form template contains the success (or failure) alert inside it (the template) lives within the @foreach that loops comments when a reply is added, the confirmation alert appears under every comment.

Of course, I want it to appear only under the comment to which a reply was given.

如何防止在这个Laravel应用中成功提示的重复出现?

How can I achieve the desired result?

NOTE

While trying to implement the solution proposed by @Alun Razvan, I get the error Undefined variable: comment in comment-form.blade.php.

英文:

I have made a blogging application in Laravel 8.

I am working on the functionality of adding comment replies.

In the controller, I add comments like this:

$comment = [
&#39;user_id&#39;    =&gt; Auth::user()-&gt;id,
&#39;article_id&#39; =&gt; $request-&gt;get( &#39;article_id&#39; ),
&#39;parent_id&#39; =&gt; $request-&gt;get( &#39;parent_id&#39; ),
&#39;body&#39;       =&gt; $fields[&#39;msg&#39;],
&#39;approved&#39;   =&gt; 0
];
// Insert comment in the &#39;comments&#39; table
$query = Comment::create( $comment );
if ( $query ) {
return redirect()-&gt;back()-&gt;with( &#39;success&#39;, &#39;Your comment is pending.&#39; );
} else {
return redirect()-&gt;back()-&gt;with( &#39;error&#39;, &#39;Adding comment failed&#39; );
}

I have added the comments form under each comment:

@foreach ($comments as $comment)
@if (null == $comment-&gt;parent_id)
&lt;li class=&quot;depth-1 comment&quot;&gt;
&lt;div class=&quot;comment__avatar&quot;&gt;
&lt;img class=&quot;avatar&quot; src=&quot;{{ asset(&#39;images/avatars/&#39; . $comment-&gt;user-&gt;avatar) }}&quot; alt=&quot;&lt;img class=&quot;avatar&quot; src=&quot;{{ asset(&#39;images/avatars/&#39; . $reply-&gt;user-&gt;avatar) }}&quot; alt=&quot;{{ $comment-&gt;user-&gt;first_name }} {{ $comment-&gt;user-&gt;last_name }}&quot; width=&quot;50&quot; height=&quot;50&quot;&gt;&quot; width=&quot;50&quot; height=&quot;50&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;comment__content&quot;&gt;
&lt;div class=&quot;comment__info&quot;&gt;
&lt;div class=&quot;comment__author&quot;&gt;{{ $comment-&gt;user-&gt;first_name }} {{ $comment-&gt;user-&gt;last_name }}&lt;/div&gt;
&lt;div class=&quot;comment__meta&quot;&gt;
&lt;div class=&quot;comment__time&quot;&gt;{{ date(&#39;jS M Y&#39;, strtotime($comment-&gt;created_at)) }}&lt;/div&gt;
@auth
&lt;div class=&quot;comment__reply&quot;&gt;
&lt;a class=&quot;comment-reply-link&quot; href=&quot;#0&quot;&gt;Reply&lt;/a&gt;
&lt;/div&gt;
@endauth
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;comment__text&quot;&gt;
&lt;p&gt;{{ $comment-&gt;body }}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
// Comments form template here!
@auth
&lt;div class=&quot;comment__reply&quot;&gt;
&lt;a class=&quot;comment-reply-link&quot; href=&quot;#0&quot;&gt;Reply&lt;/a&gt;
&lt;/div&gt;
@endauth
{{-- Comment replies --}}
@if (count($comment-&gt;replies))
&lt;ul class=&quot;children&quot;&gt;
@foreach ($comment-&gt;replies as $reply)
&lt;li class=&quot;depth-2 comment&quot;&gt;
&lt;div class=&quot;comment__avatar&quot;&gt;
&lt;img class=&quot;avatar&quot; src=&quot;{{ asset(&#39;images/avatars/&#39; . $reply-&gt;user-&gt;avatar) }}&quot; alt=&quot;&quot; width=&quot;50&quot; height=&quot;50&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;comment__content&quot;&gt;
&lt;div class=&quot;comment__info&quot;&gt;
&lt;div class=&quot;comment__author&quot;&gt;{{ $reply-&gt;user-&gt;first_name }} {{ $reply-&gt;user-&gt;last_name }}&lt;/div&gt;
&lt;div class=&quot;comment__meta&quot;&gt;
&lt;div class=&quot;comment__time&quot;&gt;{{ date(&#39;jS M Y&#39;, strtotime($reply-&gt;created_at)) }}&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;comment__text&quot;&gt;
&lt;p&gt;{{ $reply-&gt;body }}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
@endforeach
&lt;/ul&gt;
@endif
&lt;/li&gt;
@endif
@endforeach

The comments form (partials\comment-form.blade.php):

@if (session(&#39;success&#39;))
@include(&#39;themes/&#39; .$theme_directory . &#39;/partials/success&#39;)
@endif
@if (session(&#39;error&#39;))
@include(&#39;themes/&#39; .$theme_directory . &#39;/partials/errors&#39;)
@endif
&lt;form method=&quot;post&quot; action=&quot;{{ route(&#39;comment.submit&#39;) }}&quot; autocomplete=&quot;off&quot;&gt;
@csrf
&lt;fieldset&gt;
&lt;input type=&quot;hidden&quot; name=&quot;article_id&quot; value=&quot;{{ $article-&gt;id }}&quot;&gt;
&lt;input type=&quot;hidden&quot; name=&quot;parent_id&quot; value=&quot;{{ $comment-&gt;id ?? &#39;&#39; }}&quot;&gt;
&lt;div class=&quot;message form-field&quot;&gt;
&lt;textarea name=&quot;msg&quot; id=&quot;message&quot; class=&quot;h-full-width&quot; placeholder=&quot;Your Message&quot;&gt;&lt;/textarea&gt;
@error(&#39;msg&#39;)
&lt;p class=&quot;help-block text-danger&quot;&gt;{{ $message }}&lt;/p&gt;
@enderror
&lt;/div&gt;
&lt;br&gt;
&lt;input name=&quot;submit&quot; id=&quot;submit&quot; class=&quot;btn btn--primary btn-wide btn--large h-full-width&quot; value=&quot;Add Comment&quot; type=&quot;submit&quot;&gt;
&lt;/fieldset&gt;
&lt;/form&gt;

The problem

Due to the fact that the comments form template contains the success (or failure) alert inside it (the template) lives within the @foreach that loops comments when a reply is added, the confirmation alert appears under every comment.

Of course, I want it to appear only under the comment to which a reply was given.

如何防止在这个Laravel应用中成功提示的重复出现?

How can I achieve the desired result?

NOTE

While trying to implement the solution proposed by @Alun Razvan, I get the error Undefined variable: comment in comment-form.blade.php.

答案1

得分: 1

在你的控制器中,还要传递父评论的ID,就像这样:

return redirect()->back()->with([
    'success' => 'Your comment is pending.',
    'success_comment_id' => $request->get('parent_id'),
]);

然后在你的 comment-form.blade.php 模板中,检查评论ID是否匹配。

@if (session('success') && session('success_comment_id') == ($comment->id ?? null))
@include('themes/' . $theme_directory . '/partials/success')
@endif
英文:

In your controller, also pass the parent comment ID like this:

return redirect()-&gt;back()-&gt;with([
    &#39;success&#39; =&gt; &#39;Your comment is pending.&#39;,
    &#39;success_comment_id&#39; =&gt; $request-&gt;get(&#39;parent_id&#39;),
]);

Then in your comment-form.blade.php template, check if the comment IDs match.

@if (session(&#39;success&#39;) &amp;&amp; session(&#39;success_comment_id&#39;) == ($comment-&gt;id ?? null))
@include(&#39;themes/&#39; .$theme_directory . &#39;/partials/success&#39;)
@endif

huangapple
  • 本文由 发表于 2023年5月7日 19:13:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定