php - Laravel 函数之间传递变量

标签 php arrays laravel laravel-5

我想我只是在查看它,但我无法将变量显示在我的 View 中。

在同一个 Controller 中,我调用一个函数,从那里我返回一个包含变量(也是数组)的数组。在我最初启动的函数中,我将变量发送到 View 。

但是,在 View 中我收到一条错误,指出变量未定义。

我需要的信息在数组 $items 和数组 $trans 中。这些需要进入功能确认并最终出现在确认 View 中。

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

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)';

}

这是我需要访问变量的 View 部分:

<?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>

最佳答案

您必须将变量发送到 View 。您可以将代码更改为如下所示:

//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');
    }
}

希望这有帮助...

关于php - Laravel 函数之间传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40136623/

相关文章:

php - AJAX (prototype/php) 在脚本执行期间获取部分状态更新

php在html页面中创建额外的空间

jquery - Tumblr API v2 : Displaying Images?

将字符串与数组中的单词进行比较

c++ - 如何在 C++ 中接收可变长度数组的函数中传递可变长度数组?

laravel - 使用 laravel blade 限制连续 <td> 的数量

javascript - 单击按钮后将数据插入数据库

php - 插入批量 php mysql,检查重复项和字段重复项的总和值

php - MySQL 跨表查询连接

laravel - withTrashed 上的 hasManyThrough 关系