Laravelshoppingcart: Eager loading?

Created on 14 Oct 2018  路  2Comments  路  Source: Crinsane/LaravelShoppingcart

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

Most helpful comment

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

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

All 2 comments

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

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ChrisThompsonTLDR picture ChrisThompsonTLDR  路  5Comments

ellgibug picture ellgibug  路  5Comments

mbijker picture mbijker  路  4Comments

pelachile picture pelachile  路  8Comments

rubelkhan447 picture rubelkhan447  路  6Comments