[英]Cannot Access Lavavel Collection Properties In Loop
I am trying to access a collection and create a new object using part of the results. 我正在尝试访问集合并使用部分结果创建一个新对象。
I keep getting this Undefined offset: 0
and I can't seem to get around it. 我一直得到这个Undefined offset: 0
,我似乎无法解决它。
Here is my foreach loop in my controller; 这是我的控制器中的foreach循环;
foreach($payments as $payment_key => $payment_value) {
//payment_value['Vendor ZIP'] will be something like "SW1A1AA"
$partial_vendor_zip = trim_and_convert($payment_value['Vendor ZIP'], 0, -3);
//$partial_vendor_zip will be something like "SW1A"
//SQL is something like SELECT * FROM postcode_coord WHERE postcode = 'SW1A' LIMIT 1;
$postcode_coord = PostcodeCoord::where('postcode', '=', $partial_vendor_zip)->take(1)->get();
print $postcode_coord['0']->lat; //causes error; Undefined offset: 0
$markers[] = (object)[
"postcode" => $postcode_coord->postcode,
"payment_name" => $payment_value['Contract Title'],
"full_postcode" => $payment_value['Vendor ZIP'],
"lat" => $postcode_coord->lat,
"lng" => $postcode_coord->lng,
];
}
Interestingly, I can still get a print of all the "lat"s - but I also get the error. 有趣的是,我仍然可以打印所有的“ lat”-但是我也得到了错误。 If I replace that with just a print_r($postcode_coord);
如果我只用print_r($postcode_coord);
替换它print_r($postcode_coord);
then here is a sample of my collection; 这是我收藏的样品;
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Models\PostcodeCoord Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 429
[postcode] => CF11
[lat] => 51.47
[lng] => -3.20
[created_at] => 2015-02-20 13:07:01
[updated_at] => 2015-02-20 13:07:01
)
[original:protected] => Array
(
[id] => 429
[postcode] => CF11
[lat] => 51.47
[lng] => -3.20
[created_at] => 2015-02-20 13:07:01
[updated_at] => 2015-02-20 13:07:01
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
)
)
So that works! 这样行得通! But I can't reference the attributes. 但是我不能引用这些属性。 Even when I try and change my query to something like; 即使我尝试将查询更改为类似的内容,
$postcode_coord = PostcodeCoord::where('postcode', '=', $partial_vendor_zip)->take(1)->toArray();
And that just gives me an undefined method error. 这只是给我一个未定义的方法错误。
Laravel is very confusing to me... Laravel对我非常困惑。
You need to do it like this: 您需要这样做:
$postcode_coord = PostcodeCoord::where('postcode', '=', $partial_vendor_zip)->first();
print $postcode_coord->lat;
$markers[] = (object)[
"postcode" => $postcode_coord->postcode,
"payment_name" => $payment_value['Contract Title'],
"full_postcode" => $payment_value['Vendor ZIP'],
"lat" => $postcode_coord->lat,
"lng" => $postcode_coord->lng,
];
Note the use of first()
instead of take(1)->get()
, since the latter will return a collection and the former will return a single item. 请注意使用first()
代替take(1)->get()
,因为后者将返回一个集合,而前者将返回一个项目。
If you really want to use take(1)->get()
, then you have to do it like this: 如果您真的想使用take(1)->get()
,那么您必须这样做:
$postcode_coord = PostcodeCoord::where('postcode', '=', $partial_vendor_zip)->take(1)->get();
print $postcode_coord[0]->lat;
$markers[] = (object)[
"postcode" => $postcode_coord[0]->postcode,
"payment_name" => $payment_value['Contract Title'],
"full_postcode" => $payment_value['Vendor ZIP'],
"lat" => $postcode_coord[0]->lat,
"lng" => $postcode_coord[0]->lng,
];
Note here the use of 0
instead of '0'
and that this index is also used in subsequent references to $postcode_coord
. 请注意,此处使用0
而不是'0'
并且在随后对$postcode_coord
引用中也使用此索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.