无法在循环中访问Lavavel集合属性

时间:2015-05-27 15:10:29

标签: php laravel eloquent laravel-5

我正在尝试访问集合并使用部分结果创建新对象。

我不断得到这个Undefined offset: 0而我似乎无法绕过它。

这是我控制器中的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,
        ];

    }

有趣的是,我仍然可以得到所有“lat”的印刷品 - 但我也得到了错误。如果我用print_r($postcode_coord);替换它,那么这里是我的集合的样本;

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
            )

    )

)

这样有效!但我无法参考这些属性。即使我尝试将查询更改为类似的内容;

$postcode_coord = PostcodeCoord::where('postcode', '=', $partial_vendor_zip)->take(1)->toArray();

这只是给我一个未定义的方法错误。

Laravel对我很困惑......

1 个答案:

答案 0 :(得分:2)

你需要这样做:

$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,
];

请注意使用first()代替take(1)->get(),因为后者将返回一个集合,前者将返回单个项目。

如果您真的想使用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,
];

请注意使用0代替'0',并且此索引也会用于$postcode_coord的后续引用。