3
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?

0

1 Answer 1

10

$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);
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Except the keyword is 'use' instead of 'using'
Sure, no problem. If it works for you, you should tick it as answer:)
yes sir! :) Stackoverflow community is great.. i would have not been able to complete a few projects without it.. taught me a lot!

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.