【问题标题】:Image is not being added in Cart图片未添加到购物车中
【发布时间】:2017-11-08 06:28:21
【问题描述】:

我正在使用 Crinsane 购物车,我试图将图像传递给购物车,但是 我有错误

传递给 Gloudemans\Shoppingcart\Cart::add() 的参数 5 必须是 类型数组,给定字符串,调用 E:\phalwalatheme\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php 在第 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>

当我评论 $products-&gt;image 时,一切正常。并且 img div 也显示“alt”输出。我也使用过{{ $product-&gt;image}}array($products-&gt;image)。但它们都不起作用。

【问题讨论】:

  • 我认为你需要这样尝试:Cart::add(array('id' =&gt; $id, 'name' =&gt; $products-&gt;name 'qty' =&gt; 1, 'price' =&gt; $products-&gt;price));希望这对你有帮助!!
  • 但我仍然如何声明图像???
  • Cart::add(array('id' =&gt; $id, 'name' =&gt; $products-&gt;name, 'qty' =&gt; 1, 'price' =&gt; $products-&gt;price, 'image' =&gt; $products-&gt;image)); 试试这个,可能有用!
  • 是的,我没有出错,但没有显示静止图像。
  • 如果这解决了您的问题,请支持Argument 5 passed to Gloudemans\Shoppingcart\Cart::add() must be of the type array, string given

标签: php laravel laravel-5


【解决方案1】:

你的错误是说Argument 5 passed to Gloudemans\Shoppingcart\Cart::add() must be of the type array, string given 这意味着它需要array() 而你给string

尝试如下:

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

要在您的cart.blade.php 文件中显示图像,请执行以下操作:

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

希望这能解决您的问题!

【讨论】:

    【解决方案2】:

    你应该像这样使用array():

    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>
    

    确保$cartItem-&gt;image 是图片资源的链接 url 第二个参数是一个包含一个值的数组,例如 url('user/profile', [1]); 阅读 laravel url 中的 url 助手

    【讨论】:

      【解决方案3】:

      我认为你没有正确通过Crinsane shopping Cart document

      在其最基本的形式中,您可以指定要添加到购物车的产品的 ID、名称、数量和价格。 Cart::add('293ad', 'Product 1', 1, 9.99);

      作为可选的第五个参数,您可以将选项传递给它,因此您可以添加多个具有相同 id 但(例如)大小不同的项目。 Cart::add('293ad', 'Product 1', 1, 9.99, ['size' => 'large']); 所以你的代码应该是这样的: Cart::add($id,$products->name,1,$products->price,['image' => $products->image]); 希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        相关资源
        最近更新 更多