我试图使用给定ID从表中获取记录,我想将特定列存储到另一个数组中,但每次我var_dump()
我的数组时我都会获得null
。
$getOrder = array();
foreach($ordersInWaypoint as $ordersInWaypoints)
{
$getOrder[] = array(
array($ordersInWaypoints),
array(Order::where('id', '=', $ordersInWaypoints)->get()->lists('tracking_id')),
array(Order::where('id', '=', $ordersInWaypoints)->get()->lists('shipper_ref_no'))
);
}
对于那些想知道$ordersInWaypoint从哪里来的人,我从这里检索到:
$ordersInWaypoint = array();
ordersInWaypoint = Transaction::where('waypoint_id', "=", $firstWaypoint)->get()->lists('order_id');
不要介意$firstWaypoint
,因为这是由用户输入决定的。
每次我执行var_dump($getOrder);
,我得到以下结果集:
array (size=1)
0 =>
array (size=3)
0 =>
array (size=1)
0 => int 637
1 =>
array (size=1)
0 =>
array (size=1)
...
2 =>
array (size=1)
0 =>
array (size=1)
...
第一个元素是正确的,但其余的返回空集合。
我认为你没有得到任何价值,因为那些Order::where(...)
返回Eloquent对象,而不仅仅是单个值。
你最好这样试试:
$getOrder = array();
foreach($ordersInWaypoint as $ordersInWaypoints)
{
$order = Order::where('id', '=', $ordersInWaypoints)->get();
$getOrder[] = array(
array($ordersInWaypoints),
array($order->tracking_id),
array($order->shipper_ref_no)
);
}
让我知道这是否修复了它;)
编辑
如果不使用多维数组就更好了,因为只需要这三个值,所以最后的代码变成:
$getOrder = array();
foreach($ordersInWaypoint as $ordersInWaypoints)
{
$order = Order::where('id', '=', $ordersInWaypoints)->get(); // create Eloquent object from query
$getOrder[] = array(
$ordersInWaypoints,
$order->tracking_id, // Calls column value as method from the Eloquent object
$order->shipper_ref_no // Same (you could also call it as an array,
); // in that case you would type $order['shipper_ref_no']
}