Home > Mobile >  how to retrieve array index from collection?
how to retrieve array index from collection?

Time:07-20

so i want to fetch store_id to pass to view form action, but always get error that store_id attribute is not in collection

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();
    $carts = $cart->groupBy(fn ($i) => $i->Product->Store->name);

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

View

<form action="/editshipping" method="POST">
@csrf
...
</form>

Actually, I want the action like this action="/editshipping/{{ $carts->store_id }}" but still error

enter image description here

I only want to take store_id, for item1 the store_id is also the same, therefore I only want to take 1 store_id, how do I do it?

CodePudding user response:

Does this solve your problem?

$carts->first()->first()->store_id

The thing is that you have a collection in another collection and $carts is a plural name that means that you don't have a single item within it. So, your structure can be improved and cleaner.

  • Related