Php 在laravel控制器中获取循环数据

Php 在laravel控制器中获取循环数据,php,laravel,Php,Laravel,我需要循环我的购物车数据,并获取其中的部分,如:标题和价格,用于我的第三方在线支付 以下是我所拥有的: $items = Cart::getContent(); foreach($items as $item) { $item->id; // the Id of the item $item->name; // the name $item->price; // the si

我需要循环我的购物车数据,并获取其中的部分
,如:标题和价格
,用于我的第三方在线支付

以下是我所拥有的:

$items = Cart::getContent();
        foreach($items as $item)
        {
            $item->id; // the Id of the item
            $item->name; // the name
            $item->price; // the single price without conditions applied
            $item->getPriceSumWithConditions(); // the subtotal with conditions applied
            $item->quantity; // the quantity
        }

        $items = [
            array(
                'id'        => 
                'price'     => 
                'quantity'  => 
                'name'      =>
            )
        ];
我的问题是:

我应该如何填充数组?我的意思是我应该做一些像:

$id=$item->id,然后在我的数组中说
'id'=>$id
或者只说
'id'=>$item->id

PS:我知道这个问题对你们中的一些人来说可能是个愚蠢的问题,但我不能
dd
我的结果这就是为什么我在这里问,
为什么?
因为我的数据通过 api,我得到的只是没有得到结果。所以我真的不能在我的 前端。这就是我在这里问的原因

样品

更新2 我改变了我的功能,如:

$items = Cart::getContent();
        foreach($items as $item)
        {
            array(
                'id'        => $item->id,
                'price'     => $item->getPriceWithConditions(),
                'quantity'  => $item->quantity,
                'name'      => $item->name,
            );
        }
问题:是,我仍然无法获得您在上面的示例视频中看到的弹出表单

代码 这是我的完整代码:

public function payonline(){
        //   midtrans
        error_log('masuk ke snap token dri ajax');
        $midtrans = new Midtrans;

        $transaction_details = array(
            'order_id'      => uniqid(),
            'gross_amount'  => $request->input('totalPriceInTotal')
        );


        $items = Cart::getContent();
        foreach($items as $item)
        {
            array(
                'id'        => $item->id,
                'price'     => $item->getPriceWithConditions(),
                'quantity'  => $item->quantity,
                'name'      => $item->name,
            );
        }

        $orderaddress = $request->input('address_id');
        // Populate customer's shipping address
        $shipping_address = array(
            'first_name'    => $request->input('buyer_name'),
            'last_name'     => $request->input('buyer_name'),
            'address'       => $orderaddress->address,
            'city'          => $orderaddress->city->name,
            'postal_code'   => $orderaddress->postalcode,
            'phone'         => $request->input('phone'),
            'country_code'  => 'IDN'
            );

        // Populate customer's Info
        $customer_details = array(
            'first_name'      => $request->input('buyer_name'),
            'last_name'       => $request->input('buyer_name'),
            'email'           => $request->input('buyer_email'),
            'phone'           => $request->input('phone'),
            'billing_address' => $shipping_address,
            'shipping_address'=> $shipping_address
            );

        // Data yang akan dikirim untuk request redirect_url.
        $credit_card['secure'] = true;
        //ser save_card true to enable oneclick or 2click
        //$credit_card['save_card'] = true;

        $time = time();
        $custom_expiry = array(
            'start_time' => date("Y-m-d H:i:s O",$time),
            'unit'       => 'hour', 
            'duration'   => 2
        );

        $transaction_data = array(
            'transaction_details'=> $transaction_details,
            'item_details'       => $items,
            'customer_details'   => $customer_details,
            'credit_card'        => $credit_card,
            'expiry'             => $custom_expiry
        );

        try
        {
            $snap_token = $midtrans->getSnapToken($transaction_data);
            //return redirect($vtweb_url);
            echo $snap_token;
        } 
        catch (Exception $e) 
        {   
            return $e->getMessage;
        }
    }
像这样的

$items = Cart::getContent();
            $yourItems = [];
            foreach($items as $item)
            {
                $data = ['id' => '','price'=> '', 'quantity'  => '', 'name' =>'', ];

                $data = ['id'] = $item->id; // the Id of the item
                $data = ['name'] = $item->name; // the name
                $data = ['price'] = $item->price; // the single price without conditions applied
                $data = ['quantity'] =  $item->quantity; // the quantity

array_push($yourItems, $data);
            }

假设您在代码中提到的所有格式都符合“Midtrans”API规范,那么您只需将数组变量分配给项的foreach循环,如下所示:

$items = Cart::getContent();
$item_details = array();
        foreach($items as $item)
        {
            $item_details[] = array(
                'id'        => $item->id,
                'price'     => $item->getPriceWithConditions(),
                'quantity'  => $item->quantity,
                'name'      => $item->name,
            );
        }
然后用格式化的数组$item\u details替换事务数据

$transaction_data = array(
            'transaction_details'=> $transaction_details,
            'item_details'       => $item_details,
            'customer_details'   => $customer_details,
            'credit_card'        => $credit_card,
            'expiry'             => $custom_expiry
        );

如果您执行
foreach($items as&$item)
操作,您可以修改这些项,并且更改将保持不变。@ceejayoz的意思?您可以添加所需的示例输出吗?似乎您正在尝试将数组元素推送到对象上。@SagarGautam,等等。分配只能发生在可写值上。我在我的网络上得到这个
/paynline?\u=1518579844775
消息:
未定义变量:request
然后你必须检查/var\u转储你的$request以查看其中的内容,我的答案只是相应地分配你的item变量