Faiez
Faiez

Reputation: 305

Image is not being added in Cart

I am using Crinsane Shopping Cart and I was trying to pass an image to Cart, but I am having error

Argument 5 passed to Gloudemans\Shoppingcart\Cart::add() must be of the type array, string given, called in E:\phalwalatheme\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 221

CartController.php

public function addItem($id)
{
     $products=Products::find($id);

     Cart::add($id,$products->name, $products->image ,1,$products->price);
     return back();
}

cart.blade.php

<td class="product-thumbnail">
    <img src="{{url('images/product',$cartItem->image)}}" alt="product-thumbnail">
</td>

When I comment $products->image everything works fine. And img div is also showing "alt" output. I have also used {{ $product->image}} and array($products->image). But none of them worked.

Upvotes: 0

Views: 1405

Answers (3)

sumit sharma
sumit sharma

Reputation: 716

I think you are not go through Crinsane shopping Cart document properly

In its most basic form you can specify the id, name, quantity, price of the product you'd like to add to the cart. Cart::add('293ad', 'Product 1', 1, 9.99);

As an optional fifth parameter you can pass it options, so you can add multiple items with the same id, but with (for instance) a different size. Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large']); so your code should be like this: Cart::add($id,$products->name,1,$products->price,['image' => $products->image]); Hope this will help.

Upvotes: 0

Jon Awoyele
Jon Awoyele

Reputation: 356

You should use array() like:

CartController.php

public function addItem($id){
    $products=Products::find($id);

    Cart::add(array('id' => $id, 'name' => $products->name, 'qty' => 1, 
    'price' => $products->price, 'image' => $products->image));

    return back();
}

cart.blade.php

<td class="product-thumbnail">
    <img src="/images/product/{{$cartItem->image}}" alt="product-thumbnail">
</td>

make sure $cartItem->image is a link to an image resource url second parameter is an array containing a value e.g url('user/profile', [1]); read about url helpers in laravel url

Upvotes: 0

Hiren Gohel
Hiren Gohel

Reputation: 5042

Your error is saying that Argument 5 passed to Gloudemans\Shoppingcart\Cart::add() must be of the type array, string given That means it need array() and you give string.

Try like below:

Cart::add(array('id' => $id, 'name' => $products->name, 'qty' => 1, 'price' => $products->price, 'image' => $products->image));

To display image in your cart.blade.php file, do like:

<img src="{{ asset('images/product/'. $cartItem->image) }}" alt="product-thumbnail">

Hope this fixed your problem!

Upvotes: 1

Related Questions