vanilophp / cart

Cart Module for Vanilo (or any Laravel app)
https://vanilo.io
MIT License
50 stars 17 forks source link

All item from different carts shown as user's cart #10

Closed Romik90 closed 5 years ago

Romik90 commented 5 years ago

If I open /shop uri or product page, Cart::getItems() return all items from database that belongs to different carts and users. On other pages everything works fine.

dd(Cart::model()) on /shop uri show this

relations: array:2 [▼

"user" => User {#722 ▶}
"items" => Collection {#713 ▼
  #items: array:4 [▶]
}

]

on any other page:

relations: []

And ideas why?

fulopattila122 commented 5 years ago

What does your controller code on /shop URI do? What does it do on other pages?

Romik90 commented 5 years ago

@fulopattila122 it does nothing special `class ProductController extends Controller { /* @var ProductSearcher / private $productSearcher;

public function __construct(ProductSearcher $productSearcher)
{
    $this->productSearcher = $productSearcher;
}

public function index(ProductIndexRequest $request, string $taxonomyName = null, Taxon $taxon = null)
{
    $properties = PropertyProxy::get();

    if ($taxon) {
        $this->productSearcher->withinTaxon($taxon);
    }

    return view('pages.categories', [
        'products'   => $this->productSearcher->popular()->paginate(30),
        'taxon'      => $taxon,
        'properties' => $properties,
        'filters'    => $request->filters($properties)
    ]);
}`

If I dd(\Cart::getItems()) inside index method, it shows correct cart items, but if I do the same in the template, it shows me everythins that exists in cart_items table

fulopattila122 commented 5 years ago

Can you show the code you're using in the template?

Romik90 commented 5 years ago

{{ dd(Cart::model()) }} at the very beginning

fulopattila122 commented 5 years ago

Never use the Cart::model().

Here's a good example how to use the Cart facade in views: https://github.com/vanilophp/demo/blob/0.5/resources/views/cart/show.blade.php

fulopattila122 commented 5 years ago

Did that solve the problem?

Romik90 commented 5 years ago

No. I actually use cart facade as in your demo. So I did {{ dd(Cart::getItems()) }} and result is the same, I see cart items from different carts which belongs to defferent users Collection {#714 ▼

items: array:4 [▼

0 => CartItem {#720 ▶}
1 => CartItem {#719 ▶}
2 => CartItem {#718 ▶}
3 => CartItem {#717 ▶}

] } In database image image

config/vanilo.php 'cart' => [ 'extra_product_attributes' => ['weight', 'size', 'partner_id'], 'preserve_for_user' => true, ]

Romik90 commented 5 years ago

And again, if I output cart items on main page, I see correct cart with only mine items Collection {#646 ▼

items: array:2 [▶]

}

fulopattila122 commented 5 years ago

Does that route have the web middleware? Looks like you have no session.

Romik90 commented 5 years ago

I found out that in the web middleware group \Illuminate\Session\Middleware\AuthenticateSession::class was commented. After uncommenting everything works correct. Thank you so much, you saved my life!