2

I guess I'm just looking over it but I can't get my variables to my view.

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.

But, in the view I get an error saying the variable is undefined.

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.

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

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

}

This is the part of view where I need access to the variables:

<?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>
2
  • What do you get when you var_dump the array that you send to the view instead of showing the view? Is the data shown? Commented Oct 19, 2016 at 16:18
  • @TurgutSarıçam in the view I get a Laravel generated error message saying: Undefined variable: trans (View: path to the confirmation view) So I'm never getting the var_dump results. However, I first do a var_dump on $order_id and that doesn't return an error. It's second var_dump (containing trans) that causes the error. Commented Oct 19, 2016 at 16:24

1 Answer 1

3

You have to send the variables to the view. You could change the code to something like this:

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

Hope this helps...

Sign up to request clarification or add additional context in comments.

4 Comments

Can I access $trans that way? Since only few lines above, I added those to the data array which is send to the view. But the error must be in how I try to get those variables.
How are you trying to get those variables? Can you edit your post with the view code?
I've added the view code where the variables are used.
I edited the code. Try with this new one. Now I understand correctly what you're trying to do. Check if it works...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.