在laravel中的函数之间传递变量

时间:2016-10-19 16:11:11

标签: php arrays laravel laravel-5

我想我只是在看它,但我无法将我的变量放到我看来。

在同一个控制器中我调用一个函数,从那里我返回一个包含变量的数组(也就是数组' s)。在我最初启动的函数中,我将变量发送到视图。

但是,在视图中我得到一个错误,说变量未定义。

我需要的信息是数组$ items和array $ trans。这些需要进入功能确认并最终进入确认视图。

这两个函数(我试图删除大多数与问题无关的代码):

public function confirmation($order_id){
$order = Orders::findOrFail($order_id);
if(isset($order->transaction_id)){
    $data = [];
    $data['order_id'] = $order->order_reference;
    $data['trans'] = $order->dat['tr'];
    $data['items'] = $order->dat['it'];
    return view('confirmation', $data);
}else{
        //Nothing relevant 
    }
}

public function sendpicqer($order_id){
$order = Orders::with(['orderDetails', 'orderAddress', 'customer'])->where('order_id', $order_id)->first();
$order_details = OrderDetails::where('order_id', $order_id)->get();

$error = $order_id;

$result = $this->picqer->addCustomer($customer);

if(!isset($result['data'])){
    $error = $result;
    if(is_array($result)){
        $error = json_encode($result);
    }   
    return redirect()->route('cancel');
    }
    $orderData = [
        'idcustomer' => $result['data']['idcustomer']
    ];

    $orderData['products'] = [];
    $items = [];
    foreach($order_details as $od){
        $pid = $od->product_id;
        switch ($pid) {
            case 1:
                $pid = 2399983;
                break;
            case 2:
                $pid = 2399990;
                break;
        }
        $orderData['products'][] = [
            'idproduct' => $pid,
            'amount' => $od->quantity
        ];
        $items[] = [
            'sku' => $pid,
            'name' => $od->product_id->product_name,
            'price' => $od->product_id->product_price,
            'quantity' => $od->quantity
        ];
    }
    $result = $this->picqer->addOrder($orderData);
    if(isset($result['data'])){
        //Succeeded!
        $idorder = $result['data']['idorder'];
        $orderid = $result['data']['orderid'];

        $trans = array('id' => $orderid, 'affiliation' => 'Matt Sleeps', 'revenue' => $order->total_price);

        $dat = [];
        $dat['tr'] = $trans;
        $dat['it'] = $items;

        return $dat;

        $result2 = $this->picqer->sendRequest('/orders/'.$idorder.'/process', null, 'POST');
        if(!isset($result2['data'])){
            $error = $result2;
            if(is_array($result2)){
                $error = json_encode($result2);
            }
            return redirect()->route('cancel');
        }
    }else{
        $error = $result;
        if(is_array($result)){
            $error = json_encode($result);
        }   
        return redirect()->route('cancel');
    }

    //Order is successfully confirmed and send to Picqer!
    $error = '(Both to the customer and with Picqer)';

}

这是我需要访问变量的视图的一部分:

<?php
var_dump($order_id);
var_dump($trans);
var_dump($items);


// Function to return the JavaScript representation of a TransactionData object.
function getTransactionJs(&$trans) {
  return <<<HTML
ga('ecommerce:addTransaction', {
  'id': '{$trans['id']}',
  'affiliation': '{$trans['affiliation']}',
  'revenue': '{$trans['revenue']}'
});
HTML;
}

// Function to return the JavaScript representation of an ItemData object.
function getItemJs(&$transId, &$item) {
      return <<<HTML
    ga('ecommerce:addItem', {
      'id': '$transId',
      'name': '{$item['name']}',
      'sku' : '{$item['sku']}',
      'price': '{$item['price']}',
      'quantity': '{$item['quantity']}'
    });
    HTML;
    }

?>

<script>

<?php
echo getTransactionJs($trans);

foreach ($items as &$item) {
  echo getItemJs($trans['id'], $item);
}
?>

ga('ecommerce:send');
</script>

1 个答案:

答案 0 :(得分:1)

您必须将变量发送到视图。您可以将代码更改为以下内容:

//Return the view via confirmation function.
public function sendpicqer($order_id){
   ...
   return $this->confirmation($order_id, $items, $trans);
}


public function confirmation($order_id, $items, $trans){
    $order = Orders::findOrFail($order_id);
    if(isset($order->transaction_id)){
        $data = [];
        $data['order_id'] = $order->order_reference;
        $data['trans'] = $trans;
        $data['items'] = $items;

        //Send the variables to the view
        return view('confirmation', $data);
    }else{
        return redirect()->route('cancel');
    }
}

希望这会有所帮助......