mercredi 19 octobre 2016

Passing variabels between functions in laravel

I guess I'm just looking over it but I cant get my variabels to my view.

In the same controller I call a function, from there I return an array containing the variabels (also array's). From within the function I originally started I send the variabels 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 2 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 succesfully confirmed and send to Picqer!
    $error = '(Both to the customer and with Picqer)';

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire