获取数据作为响应,但在通过 ajax 渲染视图后,获取的数据很少
Getting data in response, but after rendering the view through ajax not getting few data
Laravel: 5.7.25
PHP 7.2.10
响应中的样本数据
{,…}
data: [{id: 1,…}, {id: 2,…}, {id: 3,…}, {id: 4,…}, {id: 5,…}, {id: 6,…}, {id: 7,…}, {id: 8,…}, {id: 9,…},…]
0: {id: 1,…}
carModelBodyType: {id: 1, name: "Sedan", status: 1, created_at: "2019-01-03 13:25:46", updated_at: "2019-01-03 13:25:46"}
id: 1
insuranceProvider: {id: 1, name: "The Oriental Insurance Company Ltd", has_logo: null, recommended_partner: 1,…}
base_commission_amount: 0
base_commission_percentage: 10
bonus_amount: 0
bonus_percentage: 2
has_logo: null
id: 1
name: "The Oriental Insurance Company Ltd"
planBenifits: [,…]
0: {id: 83, insurance_providers_id: 1, year: 2019, effective_date: "2019-01-27 00:00:00", plan_type: 0,…}
1: {id: 84, insurance_providers_id: 1, year: 2019, effective_date: "2019-01-27 00:00:00", plan_type: 0,…}
2: {id: 85, insurance_providers_id: 1, year: 2019, effective_date: "2019-01-27 00:00:00", plan_type: 0,…}
可见
@foreach ( $car_premium_details as $car_premium_detail )
{{ var_dump($car_premium_detail->insuranceProvider->name) }} //Getting correctly
{{ var_dump($car_premium_detail->insuranceProvider->planBenifits) }} //Getting null
@endforeach
正在使用 view()->render()
函数通过 ajax 呈现视图。
回复截图
API 资源并非真正设计用于传递给视图,它们设计用于 JSON 响应,它们被序列化为 JSON.
如果将集合对象传递给视图,则不会执行任何序列化,因此它仍然是一个资源集合class。
调用$car_premium_details->toArray(null)
可以自己序列化成数组(第一个参数应该是request对象,可以传null)
那么您必须将其作为数组而不是对象来访问。
但是,我仍然不建议尝试使用您认为的 API 资源,除非您有真正的理由这样做。在大多数情况下,传递原始模型应该就足够了,而且更容易操作。
我所做的是删除了资源,我创建了更多的关系并现在使用关系获取数据,
类似 $car_premium_detail->insuranceProvider->planBenifits
现在是CarPremium
-InsuranceProvider
关系,InsuranceProvider
-PlanBenifit
关系和InsuranceProvider
-BenefitMaster
关系。
我在做的是使用资源中的所有关系并使用视图中的数据,现在我使用视图中的所有关系并且只发送 CarPremium
eloquent 集合来查看。如果有更好的方法,请告诉我。
是的,我希望看到一个已经格式化的数据,而不是我正在做的。
Laravel: 5.7.25
PHP 7.2.10
响应中的样本数据
{,…}
data: [{id: 1,…}, {id: 2,…}, {id: 3,…}, {id: 4,…}, {id: 5,…}, {id: 6,…}, {id: 7,…}, {id: 8,…}, {id: 9,…},…]
0: {id: 1,…}
carModelBodyType: {id: 1, name: "Sedan", status: 1, created_at: "2019-01-03 13:25:46", updated_at: "2019-01-03 13:25:46"}
id: 1
insuranceProvider: {id: 1, name: "The Oriental Insurance Company Ltd", has_logo: null, recommended_partner: 1,…}
base_commission_amount: 0
base_commission_percentage: 10
bonus_amount: 0
bonus_percentage: 2
has_logo: null
id: 1
name: "The Oriental Insurance Company Ltd"
planBenifits: [,…]
0: {id: 83, insurance_providers_id: 1, year: 2019, effective_date: "2019-01-27 00:00:00", plan_type: 0,…}
1: {id: 84, insurance_providers_id: 1, year: 2019, effective_date: "2019-01-27 00:00:00", plan_type: 0,…}
2: {id: 85, insurance_providers_id: 1, year: 2019, effective_date: "2019-01-27 00:00:00", plan_type: 0,…}
可见
@foreach ( $car_premium_details as $car_premium_detail )
{{ var_dump($car_premium_detail->insuranceProvider->name) }} //Getting correctly
{{ var_dump($car_premium_detail->insuranceProvider->planBenifits) }} //Getting null
@endforeach
正在使用 view()->render()
函数通过 ajax 呈现视图。
回复截图
API 资源并非真正设计用于传递给视图,它们设计用于 JSON 响应,它们被序列化为 JSON.
如果将集合对象传递给视图,则不会执行任何序列化,因此它仍然是一个资源集合class。
调用$car_premium_details->toArray(null)
可以自己序列化成数组(第一个参数应该是request对象,可以传null)
那么您必须将其作为数组而不是对象来访问。
但是,我仍然不建议尝试使用您认为的 API 资源,除非您有真正的理由这样做。在大多数情况下,传递原始模型应该就足够了,而且更容易操作。
我所做的是删除了资源,我创建了更多的关系并现在使用关系获取数据,
类似 $car_premium_detail->insuranceProvider->planBenifits
现在是CarPremium
-InsuranceProvider
关系,InsuranceProvider
-PlanBenifit
关系和InsuranceProvider
-BenefitMaster
关系。
我在做的是使用资源中的所有关系并使用视图中的数据,现在我使用视图中的所有关系并且只发送 CarPremium
eloquent 集合来查看。如果有更好的方法,请告诉我。
是的,我希望看到一个已经格式化的数据,而不是我正在做的。