Crinsane / LaravelShoppingcart

A simple shopping cart implementation for Laravel
MIT License
3.67k stars 1.73k forks source link

Eager loading? #513

Open mrdigitalau opened 6 years ago

mrdigitalau commented 6 years ago

Hi,

Thank you for building this, it has saved me tonnes of time.

I'm using Laravel and VueJS, I am passing the cart object into VUE to show the cart, is there any way to eager load the model associated with the cart item because I can't find a way to pass the model information into Vue?

Something like Cart::content()->with('Product')

Or similar?

Cheers

belguinan commented 6 years ago

Here maybe a solution : Build a class "Transformer" so you can map Cart::content and for each item transform it to your product model, Like this,

$products = Cart::content()->each(function($item){
      $product = new Product;
      $product->id = $item->id;
      ..
      return $product;
});
rariteth commented 6 years ago

$productIds = Cart::content()->pluck('id');

$products = Product::query()->whereIn('id', $productIds)->get();
// OR
$products = Product::find($productIds);