How to take index from foreach?

Viewed 76

So, I get the problem when I try to make the code for take the store_id from foreach

View

{{-- Cart --}}
<div class="row">
<div class="col-lg-9 my-3">
    <div class="table-responsive mb-4">
        @foreach ($carts as $cart => $items)
            <h3>{{ $cart }}</h3>
            {{-- TABLE --}}
            <table class="table">
                {{-- HEAD --}}
                <thead>
                    <tr>
                        <th class="border-0 p-3 h6 title" scope="col">
                            Product
                        </th>
                        <th class="border-0 p-3 h6 title" scope="col">
                            Price
                        </th>
                        <th class="border-0 p-3 h6 title ps-5" scope="col">
                            Quantity
                        </th>
                        <th class="border-0 p-3 ps-4 h6 title" scope="col">
                            Total
                        </th>
                    </tr>
                </thead>
                {{-- CONTENT --}}
                <tbody class="border-0">
                    @foreach ($items as $cart)
                    <tr>
                        <td class="p-3 border-0">
                            <div class="d-flex align-items-center">
                                <a class="reset-anchor d-block animsition-link" 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 class="ms-3">
                                    <a class="reset-anchor animsition-link title text-decoration-none" href="/products/{{ $cart->product->slug }}">{{ $cart->product->name }}</a>
                                </div>
                            </div>
                        </td>
                        <td class="p-3 align-middle border-0">
                            <p class="mb-0 small">Rp{{ number_format(($cart->product->price * ((100 - $cart->product->discount)/100)), 0,",",".") }}</p>
                        </td>
                        <td class="p-3 align-middle border-0">
                            <form action="/update_cart" method="POST">
                                @csrf
                                <div class="row">
                                    <div class="col-5">
                                        <input type="hidden" name="cart" value="{{ $cart->id }}">
                                        <input type="number" name="quantity" value="{{ $cart->qty }}" class="w-100 ms-5" min="0" max="{{ $cart->product->stock }}">
                                    </div>
                                </div>
                                <button type="submit" class="btn btn-sm btn-dark ms-5    mt-2" style="margin-right:20px">Update</button>
                            </form>
                        </td>
                        <td class="p-3 align-middle border-0">
                            <p class="mb-0 small">Rp{{ number_format($cart->total_product, 0,",",".") }}</p>
                        </td>
                        <td class="p-3 align-middle border-0">
                            <form action="/delete_cart" method="post">
                                @csrf
                                <input type="hidden" name="id" id="id" value="{{ $cart->id }}">
                                <a class="reset-anchor">
                                    <button type="submit" class="btn btn-link">
                                        <img src="{{ asset('img/customer/bx-trash.svg') }}" width="20">
                                    </button>
                                </a>
                            </form>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
            <hr>
            {{-- TOTAL --}}
            <div>
                <h6 class="text-md-end">Total : Rp{{ $items->sum('total_product') }}</h6>
            </div>
            {{-- SHIPPING --}}
            <div class="text-md-end">
                <a class="btn btn-dark" href="/shipping/{{ $items->store_id }}"> checkout <i class='bx bx-basket'> </i> </a>
            </div>

        @endforeach

    </div>
</div>

so in <a class="btn btn-dark" href="/shipping/{{ $items->store_id }}"> checkout <i class='bx bx-basket'> </i> </a> this is error

The Controller

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
        ]);
    }

this result when using dd($carts)

enter image description here enter image description here

This is my view

enter image description here *note : store name, product and pictures are for study only, and the url before I add $items->store_id

So the story is, I'm only going to checkout for carts with the same store_id, but I'm confused about how to pull the store_id. How to solve it?

0 Answers
Related