Home > Software engineering >  View not what I want
View not what I want

Time:07-20

So, I get the problem when I want to show the cart base on the store. I don't really know whether my database design is wrong or my controller.

This is the database

enter image description here

Contoller

public function index(Request $request)
    {
        $itemuser = $request->user();
        $cartdetail = CartDetail::where('user_id', $itemuser->id)->get();
        $cart = Cart::where('user_id', $itemuser->id)->get();

        return view('customer.cart', [
            'title' => 'Cart',
            'carts' => $cart,
            'cartdetail' => $cartdetail
        ]);
    }

View

{{-- Cart --}}
<div >
   <div >
      <div >
          @foreach ($carts as $cart)
             <h3>{{ $cart->Product->Store->name }}</h3>
             {{-- TABLE --}}
             <table >
                {{-- HEAD --}}
              <thead>
                <tr>
                    <th  scope="col">
                                            Product
                    </th>
                    <th  scope="col">
                                            Price
                    </th>
                    <th  scope="col">
                                            Quantity
                    </th>
                    <th  scope="col">
                                            Total
                    </th>
                 </tr>
              </thead>
              {{-- CONTENT --}}
              <tbody >
                  @foreach ($carts as $cart)
                     <tr>
                         <td >
                            <div >
                                <a  href="/products/{{ $cart->product->slug }}">
                                  @if($cart->product->image)
                                     <img src="img/admin_store/{{ $cart->product->image }}" width="70" />
                                  @else
                                     <img src="{{ asset('img/customer/img-1.png') }}" width="70" />
                                  @endif
                                </a>
                                <div >
                                    <a  href="/products/{{ $cart->product->slug }}">{{ $cart->product->name }}</a>
                                </div>
                             </div>
                          </td>
                          <td >
                              <p >Rp{{ number_format(($cart->product->price * ((100 - $cart->product->discount)/100)), 0,",",".") }}</p>
                          </td>
                          <td >
                              <form action="/update_cart" method="POST">
                                                @csrf
                                   <div >
                                        <div >
                                             <input type="hidden" name="cart" value="{{ $cart->id }}">
                                             <input type="number" name="quantity" value="{{ $cart->qty }}"  min="0" max="{{ $cart->product->stock }}">
                                         </div>
                                   </div>
                                   <button type="submit"  style="margin-right:20px">Update</button>
                                </form>
                             </td>
                             <td >
                                 <p >Rp{{ number_format($cart->total_product, 0,",",".") }}</p>
                             </td>
                             <td >
                                 <form action="/delete_cart" method="post">
                                                @csrf
                                      <input type="hidden" name="id" id="id" value="{{ $cart->id }}">
                                      <a >
                                         <button type="submit" >
                                                        <img src="{{ asset('img/customer/bx-trash.svg') }}" width="20">
                                         </button>
                                      </a>
                                   </form>
                               </td>
                            </tr>
                       @endforeach
                  </tbody>
              </table>
         @endforeach
     </div>
</div>

This is the result

this product *note = store name, product and pictures are for study only

My expectation is that each cart contains products that match the store, but the result is that the product loops don't match the store. How to solve it?

CodePudding user response:

I am not sure why a cart would only have 1 product, usually a shopping cart has multiple items, but I guess carts is "cart items" in reality.

You can group this Collection of "carts" by the store name then you can iterate that Collection (which would be iterating the stores with the items from that store in each Collection) then you can iterate through the items you have grouped for each store:

In your Controller:

...
$carts = $cart->groupBy(fn ($i) => $i->Product->Store->name);

In your view:

@foreach ($carts as $store => $items)
    ...
    {{ $store }} // store name
    ...
    @foreach ($items as $item) // iterate each item that was grouped by store
        ...
        {{ $item->Product->name }}
        ...
    @endforeach
@endforeach

Laravel 9.x Docs - Collections - Available Methods - groupBy

  • Related