laravel question: duplicate message display when I click the button

897 views php
-2

I want to return the message when I click the add button. Yes, indeed it show the message. But, in my case is have 3 image shown on the same page, so the problem is show the 'add to cart successfully' for the 3 image when I click the button.

Below is the controller file, but i think is done for here.

public function addItem($id){
  $pro = products::find($id);
  Cart::add(['id' => $pro->id, 'name' => $pro->pro_name,
  'qty' => 1, 'price' => $pro->pro_price,
'options' =>[
  'img' => $pro->pro_img
  ]]);
    return back()->with('status', 'add to cart successfully');
}

Should be need to make changes on the view file, below is the view file:

@foreach($data as $p)

              <article class="product-grid-item product-block">
                  <figure class="product-item-thumbnail">
                      <a href="{{url('/productsDetails')}}/{{$p->pro_name}}">
                          <img
                                  src="{{asset('/img/')}}/{{$p->pro_img}}" alt=""
                                  width="400px" height="360px" />
                          <div class="product-item-mask">
                              <div class="product-item-actions">
                                  <button class="button button-secondary button-short">
                                      Quick View
                                  </button>

                                  <a href="{{url('/cart/add')}}/{{$p->id}}" class="button add-cart-cat button--small card-figcaption-button">Add to Cart</a>
                              </div>
                          </div>
                      </a>
                  </figure>


                  <div class="product-item-details">
                      {{--Add Cart Message--}}
                      @if (session('status'))
                          <div class="alert alert-success">
                              {{ session('status') }}
                          </div>
                      @endif
                      {{--End of Add Cart Message--}}
                      <div class="product-item-brand"><label><a href="#" alt="">{{$p->pro_name}}</a></label></div>

                      <h5 class="product-item-title">
                           {{$p->pro_code}}
                      </h5>

                      <!-- snippet location product_rating -->
                      <div class="product-item-price" data-product-price="JPY1,650">
                          <div>

                              <div class="product-price-line" data-product-price-wrapper="without-tax">
                                  {{--<span class="price-rrp highlight">RM-</span>--}}
                                  <meta itemprop="price" content="1650">
                                  <meta itemprop="priceCurrency" content="">
                                  <meta itemprop="availability" content="">
                                  <meta itemprop="itemCondition" itemtype="http://schema.org/OfferItemCondition" content="http://schema.org/Condition">
                                  <span class="price-value"> RM{{$p->pro_price}}</span>
                              </div>

                          </div>
                      </div>
                  </div>
              </article>
          @endforeach

The result display shown as below: It should be coming out for display 1 message per click and not all of the message showing out.

Image of the result

answered question

1 Answer

12

From your $p in $data, can we get that item is in cart or not. If not, get that data in $p first and, then check before this code,

{{--Add Cart Message--}}
    @if($p->is_cart_added)
       @if (session('status'))
          <div class="alert alert-success">
          {{ session('status') }}
          </div>
       @endif
    @endif

posted this

Have an answer?

JD

Please login first before posting an answer.