phplaraveleloquentlaravel-viewslaravel-eloquent-resource

Getting data in response, but after rendering the view through ajax not getting few data


Laravel: 5.7.25
PHP 7.2.10

Sample data in response

{,…}
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,…}

In view

@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 is getting rendered through ajax using view()->render() function.

Screenshot of response enter image description here


Solution

  • What I did is I removed the resource, I created few more relationships and using the relationship to get data now,

    something like $car_premium_detail->insuranceProvider->planBenifits

    is now CarPremium - InsuranceProvider relationship, InsuranceProvider- PlanBenifit relationship and InsuranceProvider - BenefitMaster relationships.

    What I was doing is using all the relationships in resource and using the data in views, now I using all the relationships in views and only sending CarPremium eloquent collection to view. if there is any better way, please let me know.

    Yes would have liked a already formatted data in view instead of what I am doing.