0
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

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105

3 Answers3

0

API resources are not really designed to be passed to the view, they are designed to be used in JSON responses where they are serialized to JSON.

If you pass the collection object to the view, there won't be any serialization performed, so it'd still be a resource collection class.

You can serialize it to an array yourself by calling $car_premium_details->toArray(null) (the first parameter is supposed to be a request object, but you can pass null).

Then you'd have to access this as an array, not an object.

However, I still wouldn't recommend trying to use API resources in your view unless you have real reason to do so. In most cases, passing the original model should be sufficient and much easier to manipulate.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
0

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.

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
  • Right, that's pretty much what I recommended you to do (see the chat and the last few sentences of my answer). API resources are for API responses. The view is where you format the data for an HTML response, you shouldn't need to format data in the controller. – Devon Bessemer Feb 12 '19 at 15:21
  • @Devon is this correct to use the relationships in views, can not we format the data before sending it to view? – Prafulla Kumar Sahu Feb 12 '19 at 15:22
  • What's the point of the view if you're not going to use it to format the data? – Devon Bessemer Feb 12 '19 at 15:23
  • @Devon but is it correct to call all the relationships there? – Prafulla Kumar Sahu Feb 12 '19 at 15:23
  • Are you calling relationships or just accessing the model structure? If you eager load the data in your controller and then access the relation through the model structure, that's the right way to go about it. – Devon Bessemer Feb 12 '19 at 15:25
  • @Devon I am doing something like `$carPremium->xyz->zyx`. – Prafulla Kumar Sahu Feb 12 '19 at 15:26
  • @Devon There is also something like `$carPremium->getXyz($x)` stuffs. is that okay too? – Prafulla Kumar Sahu Feb 12 '19 at 15:29
  • That's opinion based. I think using getters is perfectly fine, you just don't want to mix business logic into the view. – Devon Bessemer Feb 12 '19 at 15:31
  • @Devon I was worried about these two things, I was using the relationships and getter methods in all my projects, but always had a thought, if this can be improved, that is why I thought as Jeffry way descibe in laracast resource is only formatting data, so I thought I should try to use that so that I will not have to do this in view, actually I still think if there is a way, it will be better. Any way as you said it is perfectly, that makes me feel little bit okay about it for now at least. :D – Prafulla Kumar Sahu Feb 12 '19 at 15:35
  • 1
    Yeah, it's really opinion based there. Some argue for complete decoupling of your view layer from your application layer, but that really can't be accomplished with Blade anyways. If you're concerned about decoupling, it's best not to use Blade at all. However, if you're going to use Blade, my opinion is to not go overboard with worrying about how you access the data. – Devon Bessemer Feb 12 '19 at 15:49
0

you can use resolve();

example:

My resource:

public function toArray($request): array
    {
        return [
            'id' => $this->id,
            'is_active' => $this->is_active,
            'name' => $this->name,
            'sort' => $this->sort,
            'branch_language_instruction_id' => $this->branch_language_instruction_id,
            'address' => $this->address,
            'address_second' => $this->address_second,
            'stop_message' => $this->stop_message,
            'city' => $this->city,
            'state_province' => $this->state_province,
            'postal_code' => $this->postal_code,
            'time_zone_lookup_id' => $this->time_zone_lookup_id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'deleted_at' => $this->deleted_at,
            'branch_language_id' => $this->branch_language_id,
            'branch_name' => (string) $this->languageTextName->translation->content ??'not found',
        ];
    }

in Controller:

$branches = collect(BranchResource::collection(Branch::get())->resolve());
        return view('superAdmin.branches.branch_permission', compact('branches' ));

In View:

<select class="form-select" id="inputGroupSelectBranch">

                                @foreach($branches as $branch)
                                    <option value="1" selected="">Choose...</option>
                                    <option class="select-branch"
                                            value="{{$branch['id']}}">{{ $branch['branch_name'] }} </option>
                                @endforeach
                            </select>