Php Laravel雄辩的关系不适用于两个数据透视表

Php Laravel雄辩的关系不适用于两个数据透视表,php,laravel-5.6,Php,Laravel 5.6,大家好,我是新来拉威尔的,我正在尝试获取数据。使用状态ID,请解释我与所有模型之间的关系,并举例说明我有这样的表 1-状态表 1-id 双名 2-城市表 1-id 双名 3-州/市数据透视表 1-id 2-状态识别码 3-城市标识 4-位置表 1-id 双名 5-城市位置透视表 1-id 2-城市标识 3-位置标识 6-共线表 1-id 2-pincode 7-位置夹码表 1-id 2-位置标识 3-1代码\u id 这是我的控制器 $states_with_cities = $states_w

大家好,我是新来拉威尔的,我正在尝试获取数据。使用状态ID,请解释我与所有模型之间的关系,并举例说明我有这样的表

1-状态表

1-id

双名

2-城市表

1-id

双名

3-州/市数据透视表

1-id

2-状态识别码

3-城市标识

4-位置表

1-id

双名

5-城市位置透视表

1-id

2-城市标识

3-位置标识

6-共线表

1-id

2-pincode

7-位置夹码表

1-id

2-位置标识

3-1代码\u 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,
第一个map函数中的$location是它当前正在迭代的对象的键。见: 因此,在第32行中,您尝试调用键变量的属性,该属性可能是“0”或“1”或其他内容。由于这不是一个对象,它将导致您得到的错误

此外,尝试映射locationName属性也无法按预期工作。locationName是一个属性,而不是一个有说服力的集合

您可能应该这样尝试:

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

第32行到底是哪一行?@Clemenz'location'=>$location->locationName->mapfunction$locationName use$location{谢谢,但我仍然收到错误消息:参数太少,无法使用函数illumb\\Support\\Collection::get,在第21行的D:\\xampp\\htdocs\\samudaay backend test\\app\\Http\\Controllers\\API\\StateCityController.php中传递0,这是第21行代码$states\u with_cities=$states\u with_cities->load['cities.cityName','location.locationName']->where'id',1->get;尝试获取非对象的属性'locationName',实际上是一个不同的问题。但不管怎样。您的$states\u with\u cities变量是如何定义的?where函数中的id,是states\u with\u cities模型的主键吗?也许您应该这样做:$states\u with\u cities=ModelName::find1->load['cities.cityName','location.locationName'];用模型名称替换模型名称
'location' => [
        'location_id' => $location->location_id,
        'location_name' => $location->name
    ];
})