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>
var_dumpthe array that you send to the view instead of showing the view? Is the data shown?