zack zack
zack zack

Reputation: 207

Photo don't display in shop cart LARAVEL 6

i'm using laravel in my project , so when i add a product in the shop cart all the data is displayed except the product image.

This is the cartcontroller.php:

             public function add(Request $request) {
             $produit=productmodel::find($request->id);
             Cart::add(array(
             'id' =>$request->id, // inique row ID
             'name' =>$request->product_name,
             'price' => $request->product_price,
             'quantity' =>$request->product_quantity,
             'attributes' => array('photo'=>$request->product_image)));
             return redirect ('shop-cart');
}

and this is the shop-cart.blade.php

  <tbody>
                                @foreach(\Cart::getContent() as $item)

                                 <tr>
                                <td class="cart__product__item">
                                  <div class="cart__product__item__title"> 
                                    <img src="{{asset('storage/product/September2020/'.$item->attributes['photo'])}}" alt=""> 
                                        <h6> {{Str::words($item->name,20) }}</h6>
                                        @foreach($item->attributes as $key  => $value)
                                        <dl class="dlist-inline small">
                                            <dt>{{ ucwords($key) }}: </dt>
                                            <dd>{{ ucwords($value) }}</dd>
                                        </dl>
                                         @endforeach
                                       
                                    </div>
                                </td>
                                <td class="cart__price"> {{$item->price}} TND</td>
                                <td class="cart__quantity">
                                        {{ $item->quantity }}
                                </td>
                                <td class="cart__total"> {{ $item->price * $item->quantity }} TND</td>
                                <td class="cart__close"><a href="{{ route('checkout.cart.remove', $item->id) }}"><i class="fa fa-times"></i> </a>
                                </td>
                              </td>
                            </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                @endif

Upvotes: 0

Views: 145

Answers (1)

nipesh pant
nipesh pant

Reputation: 46

If you are using darryldecode/cart for cart. You can go to your vendor folder and make some slight changes to add method of Cart.php file.

 public function add($id, $name = null, $price = null, $quantity = null, $image = null, $attributes = array(), $conditions = array(), $associatedModel = null)
{
    // if the first argument is an array,
    // we will need to call add again
    if (is_array($id)) {
        // the first argument is an array, now we will need to check if it is a multi dimensional
        // array, if so, we will iterate through each item and call add again
        if (Helpers::isMultiArray($id)) {
            foreach ($id as $item) {
                $this->add(
                    $item['id'],
                    $item['name'],
                    $item['price'],
                    $item['quantity'],
                    $item['image'],
                    Helpers::issetAndHasValueOrAssignDefault($item['attributes'], array()),
                    Helpers::issetAndHasValueOrAssignDefault($item['conditions'], array()),
                    Helpers::issetAndHasValueOrAssignDefault($item['associatedModel'], null)
                );
            }
        } else {
            $this->add(
                $id['id'],
                $id['name'],
                $id['price'],
                $id['quantity'],
                $id['image'],
                Helpers::issetAndHasValueOrAssignDefault($id['attributes'], array()),
                Helpers::issetAndHasValueOrAssignDefault($id['conditions'], array()),
                Helpers::issetAndHasValueOrAssignDefault($id['associatedModel'], null)
            );
        }

        return $this;
    }

    $data = array(
        'id' => $id,
        'name' => $name,
        'price' => Helpers::normalizePrice($price),
        'quantity' => $quantity,
        'image'=>$image,
        'attributes' => new ItemAttributeCollection($attributes),
        'conditions' => $conditions
    );

    if (isset($associatedModel) && $associatedModel != '') {
        $data['associatedModel'] = $associatedModel;
    }

    // validate data
    $item = $this->validate($data);

    // get the cart
    $cart = $this->getContent();

    // if the item is already in the cart we will just update it
    if ($cart->has($id)) {

        $this->update($id, $item);
    } else {

        $this->addRow($id, $item);
    }

    $this->currentItemId = $id;

    return $this;
}

Now you can simply store image in cart as below

$userId = auth()->user()->id;
    \Cart::session($userId)->add(array(
        'id' => $request->id,
        'name' =>$request->item_name,
        'price' =>$request->item_price,
        'quantity' => $request->quantity,
        'image'=>$request->image,
        'attributes' => array(),
    ));

And view your stored image from path like

  @foreach(Cart::session(auth()->user()->id)->getContent() as $items)
    <div class="row pt-5">
      <div class="col-md-3 offset-md-2">
      <img class="card-img-top" src="{{asset('photos').'/'.$items->image}}" 
          style="height:120px; width:120px;"alt="Card image cap">
      </div>
      <div class="col-md-6 ">
          <h5 class="font-weight-bold">{{$items->name}}</h5>
            Rate: Rs {{$items->price}}<br>
            Qty: {{$items->quantity}}<br>
            <?php
              $price="";
              $price=$items->quantity*$items->price;
            ?>
            Price: Rs {{$price}}<br>
            <a href="{{route('cart.destroy',$items->id)}}"><button class="btn-sm btn-outline-danger"><i class="far fa-trash-alt"></i></button></a>
      </div>
    </div>
    <hr>
 @endforeach

Upvotes: 1

Related Questions