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?

4

1 回答 1

10

$orderId不是全局变量,而是父函数的变量。尝试这个:

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

代替:

function($query)
    {   global $orderId;
        $query->where('id', '=', $orderId);
    }
于 2013-01-30T22:39:35.517 回答