Passing variables between functions in laravel
<p>I guess I'm just looking over it but I can't get my variables to my view.</p>

<p>In the same controller I call a function, from there I return an array containing the variables (also array's). From within the function I originally started I send the variables to the view.</p>

<p>But, in the view I get an error saying the variable is undefined.</p>

<p>The information I need is in array $items and array $trans. These need to get in the function confirmation and end up in the confirmation view.</p>

<p>The two functions (I tried to remove most code that has nothing to do with the question): </p>

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

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

$error = $order_id;

$result = $this-&gt;picqer-&gt;addCustomer($customer);

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

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

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

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

        return $dat;

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

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

}
</code></pre>

<p>This is the part of view where I need access to the variables:</p>

<pre><code>&lt;?php
var_dump($order_id);
var_dump($trans);
var_dump($items);


// Function to return the JavaScript representation of a TransactionData object.
function getTransactionJs(&amp;$trans) {
  return &lt;&lt;&lt;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(&amp;$transId, &amp;$item) {
      return &lt;&lt;&lt;HTML
    ga('ecommerce:addItem', {
      'id': '$transId',
      'name': '{$item['name']}',
      'sku' : '{$item['sku']}',
      'price': '{$item['price']}',
      'quantity': '{$item['quantity']}'
    });
    HTML;
    }

?&gt;

&lt;script&gt;

&lt;?php
echo getTransactionJs($trans);

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

ga('ecommerce:send');
&lt;/script&gt;
</code></pre>