Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Variable scope issue laravel

public function action_detail($orderId)
{
    $customerWithOrderDetails = Customer::with(array('order' => function($query)
    {   global $orderId;
        $query->where('id', '=', $orderId);
    }, 'order.orderdetail', 'order.attachment'))->find(Auth::user()->id);
    return var_dump($customerWithOrderDetails);
}

I am getting "variable undefined" error. Why?

like image 584
Lucky Soni Avatar asked Feb 11 '26 01:02

Lucky Soni


1 Answers

$orderId is not a global variable, but a variable of a parent function. Try this:

function($query) use ($orderId)
    {
        $query->where('id', '=', $orderId);
    }

instead of:

function($query)
    {   global $orderId;
        $query->where('id', '=', $orderId);
    }
like image 162
Rok Kralj Avatar answered Feb 12 '26 15:02

Rok Kralj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!