Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

movepixels's avatar

API Resource Issue

I cant quite figure this out.

User hasOne Contact (model that stores the User's contact info)

Now a new User obviously has no record so $user->contact returns nothing as expected.

Yet I am Passing it thru a API Resource and still return nothing. I figured it would still return something for my front-end?

'contact' => new PublicContactResource($this->contact)

But it never hits the PublicContactResource ?

Snip:

public function toArray($request)
  {
	\Log::info('Hello i am here');
   }
........

I still need to return the attributes for that table even if all are empty.

return [
      'type' => 'contact',
      'meta' => [],
      'attributes' => [
        'description' => $this->when($this->description, $this->description),
        'email' => $this->email,
        'phone' => $this->phone,
        'phone_verified' => $this->phone_verified,
        'email_verified' => $this->email_verified,
      ]
    ];

Basically return the response even if there is no record, beciase the front end needs those fields to determin how to function.

Any ideas?

0 likes
10 replies
piljac1's avatar
piljac1
Best Answer
Level 28

My guess is that somewhere in the stack, there's a protection to avoid calling toArray on a null resource to avoid some Trying to get property 'property_name' of non-object warnings/errors. Have you tried giving a new model as a fallback?

'contact' => new PublicContactResource($this->contact ?? new Contact)
1 like
piljac1's avatar

What exactly you don't understand? Where to put this code or what ?? means? If you're talking about ??, it's called the null coalescing operator, which basically is the short version of:

isset($this->contact) ? $this->contact : new Contact
movepixels's avatar

Sorry, very much appreciate your help, I just do not understand what your solution suggestion is?

I tried to implanted a wacky workaround with something like this inside the PublicContact Resource

if(!$profile->contact){
      \Log::info('******** HELLO *********');
      return [
        'type' => 'contact',
        'meta' => [
          'name' => $profile->name
        ],
        'attributes' => [
        ...... faked empty array of attributes
        ]
      ];
    }

But it still never gets this far. Hello never shows up

Calling it like:

new PublicContactResource($this->contact);

Just never hits the actual destination. Because there is no $this->contact it just returns nothing?

movepixels's avatar

I just set it as your advise:

'contact' => new PublicContactResource($this->contact ?? new Contact),

And still its giving errors

message: "Trying to get property 'description' of non-object"

I stripped everything out to bare bones:

<?php

namespace App\Http\Resources\Profile\Contacts;

use Illuminate\Http\Resources\Json\JsonResource;

class PublicContactResource extends JsonResource
{
  /**
   * Transform the resource into an array.
   *
   * @param  \Illuminate\Http\Request  $request
   * @return array
   */
  public function toArray($request)
  {
    $profile = \Route::current()->parameter('profile');

    return [
      'type' => 'contact',
      'meta' => [
        'name' => $profile->name
      ],

    ];
  }
}
piljac1's avatar

So just to clarify, using this:

'contact' => new PublicContactResource($this->contact ?? new Contact),

With your original content:

return [
    'type' => 'contact',
    'meta' => [],
    'attributes' => [
        'description' => $this->when($this->description, $this->description),
        'email' => $this->email,
        'phone' => $this->phone,
        'phone_verified' => $this->phone_verified,
        'email_verified' => $this->email_verified,
    ]
];

Gives you Trying to get property 'description' of non-object?

movepixels's avatar

I added in your suggestion :

'contact' => new PublicContactResource($this->contact ?? new \App\Models\ProfileContacts),

Doing some logging I am getting this:

[2021-09-07 00:50:04] development.INFO: array (
  'type' => 'contact',
  'meta' => 
  array (
    'name' => 'Good Stuff',
  ),
  'attributes' => 
  array (
    'email' => NULL,
    'phone' => NULL,
    'phone_verified' => NULL,
    'email_verified' => NULL,
  ),

So your suggestion has worked... some reason still thrown errors on my end but you solved my issue so greatly appreciated your time and assistance :)

I will try to sort out what is going on but for now all good!

Thanks!

movepixels's avatar

Thanks again! Learn something new everyday! Good looking out! Have a good one :)

piljac1's avatar

You too! Please consider marking the answer that helped you solve your issue as best answer :)

Please or to participate in this conversation.