Laravel Eloquent 关系不适用于两个枢轴 table

Laravel Eloquent Relationships not work with two pivot table

大家好,我是 laravel 的新成员,我正在尝试获取 data.using 状态 ID,请向我解释所有具有相互关系的模型,例如我有 table 之类的这个

1- 状态 table

1-id

2-姓名

2个城市table

1-id

2-姓名

3-state_cities枢轴table

1-id

2-sate_id

3-city_id

4- 个位置 table

1-id

2-姓名

5-city_locations枢轴table

1-id

2-city_id

3-location_id

6-密码table

1-id

2-pincode

7- location_pincodes table

1-id

2-location_id

3-pinecode_id

这是我的控制器

$states_with_cities = $states_with_cities->load(['cities.cityName','location.locationName'])->where('id',1)->get();

        $states_with_cities->transform(function($states_with_cities) {
            return [
                    'state_id' => $states_with_cities->id,
                    'state_name' => $states_with_cities->name,
                    'cities' => $states_with_cities->cities->map(function($cities,$location) {

                        return [
                            'city_id' => $cities->city_id,
                            'city_name' => $cities->cityName->name,
                            'location' => $location->locationName->map(function($locationName) use($location) {
                                return [
                                    'location_id' => $location->location_id,
                                    'location_name' => $locationName->locationName->name
                                ];
                            })
                        ];
                    }),
                ];
        });

这是我得到的错误

"message": "Trying to get property of non-object",
            "exception": "ErrorException",
            "file": "D:\xampp\htdocs\samudaay-backend\app\Http\Controllers\API\PincodeController.php",
            "line": 32,
$states_with_cities = $states_with_cities->load(['cities.cityName','location.locationName'])->where('id',1)->get();

$states_with_cities->transform(function($states_with_cities) {
    return [
        'state_id' => $states_with_cities->id,
        'state_name' => $states_with_cities->name,
        'cities' => $states_with_cities->cities->map(function($cities,$location) {
        // Location is the 'key' of the object in the collection. So it probably will be something like '0' or '1'.

            return [
                'city_id' => $cities->city_id,
                'city_name' => $cities->cityName->name,
                'location' => $location->locationName->map(function($locationName) use($location) {
                    //What you actually do here is: 0->locationName->map(...). This will result in your error
                    return [
                        'location_id' => $location->location_id,
                        'location_name' => $locationName->locationName->name
                    ];
                 })
             ];
         }),
    ];
});

第一个map函数中的$location是当前正在迭代的对象的key。 (参见:https://laravel.com/docs/5.6/collections#method-map) 因此,在第 32 行,您试图在键变量(可能是“0”或“1”或其他东西)上调用 属性。因为它不是对象,所以会导致错误.

此外,尝试映射 locationName 属性 不会按预期工作。 locationName 是 属性 而不是 eloquent 集合。

您或许应该这样尝试:

'location' => [
        'location_id' => $location->location_id,
        'location_name' => $location->name
    ];
})