如何在laravel中的数组数据中迭代数组,输出如下

输出


array:2 [▼

  0 => array:1 [▼

    0 => array:12 [▼

      "product_name" => "Raw Mango"

      "product_image" => "Raw_Mango_1997.jpg"

      "product_id" => "15"

      "variation_id" => "33"

      "original_price" => "15.31"

      "selling_price" => "12.25"

      "discount_price" => "3.06"

      "discount_percent" => "20"

      "product_stock" => "available"

      "product_unit" => "gram"

      "product_unit_value" => "250"

      "cart_quantity" => "1"

    ]

  ]

  1 => array:1 [▼

    0 => array:12 [▼

      "product_name" => "Banana - Yelakki"

      "product_image" => "Banana_-_Yelakki_9339.jpg"

      "product_id" => "20"

      "variation_id" => "41"

      "original_price" => "56.25"

      "selling_price" => "45"

      "discount_price" => "11.25"

      "discount_percent" => "20"

      "product_stock" => "available"

      "product_unit" => "gram"

      "product_unit_value" => "500"

      "cart_quantity" => "1"

    ]

  ]

]

如何在laravel刀片页面中显示这种类型的数据。我想在对象中获取数据以在laravel视图页面中显示数据。我尝试了很多方法但没有得到解决方案请帮助我


购物车控制器.php


public function viewCart(){


        $select=DB::table('carts')->where('user_id','=',Session::get('user_id'))->get();

        $count=count($select);

        if($count>0)

        {

            foreach($select as $row1)

            {

                $product_id     =   $row1->product_id;

                $variation_id   =   $row1->variation_id;

                $quantity       =   $row1->quantity;


                $sql=DB::table('products')->where('id','=',$product_id)->get();

                $arr=array();

                foreach($sql as $res)

                {

                    $product_name   =   $res->product_name;

                    $image          =   $res->product_image;

                    $product_desc   =   $res->product_desc;

                    $sql1=DB::table('product_details')->where('product_id','=',$product_id)->where('id','=',$variation_id)->get();

                    foreach($sql1 as $res1)

                

我试图在页面上获取用户添加的购物车详细信息,但是当尝试 foreach 时,它会出错。如何在 laravel 视图页面中迭代这种类型的数据,请帮助我。


绝地无双
浏览 191回答 1
1回答

千万里不及你

使用现在的代码,这些是正在执行的查询(由Laravel Debugbar显示给我):select * from `carts` where `user_id` = 1select * from `products` where `id` = 15select * from `product_details` where `product_id` = 15 and `id` = 33select * from `products` where `id` = 20select * from `product_details` where `product_id` = 20 and `id` = 41它看起来像正在为每一个执行一个单独的查询product和product_details记录; 效率不高。让我们试试这个:$carts = Cart::where('user_id', Session::get('user_id'))    ->with('product.details')    ->get();对于上述语句,需要以下Eloquent关系:// Cart modelpublic function product() { return $this->belongsTo('App\Product'); }// Product modelpublic function details() {    return $this->hasOne('App\ProductDetails', 'product_id', 'id');}这看起来好多了;无论检索多少产品或产品详细信息记录,执行的查询都不超过 3 个。select * from `carts` where `user_id` = 1select * from `products` where `products`.`id` in (15, 20)select * from `product_details` where `product_details`.`product_id` in (15, 20)现在将模型属性与其关系的属性结合起来:$data = [];foreach ($carts as $key => $cart) {    $data[$key] = [        'product_name' => $cart->product->product_name,        'product_image' => $cart->product->product_image,        'product_id' => $cart->product_id,        'variation_id' => $cart->variation_id,        'original_price' => $cart->product->details->original_price,        'selling_price' => $cart->product->details->selling_price,        'discount_price' => $cart->product->details->discount_price,        'discount_percent' => $cart->product->details->discount_percent,        'product_stock' => $cart->product->details->product_stock,        'product_unit' => $cart->product->details->product_unit,        'product_unit_value' => $cart->product->details->product_unit_value,        'cart_quantity' => $cart->quantity,    ];}
打开App,查看更多内容
随时随地看视频慕课网APP