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

rachnasundriyal33's avatar

laravel jsonresource is not returning child even though its loaded in model

I have Client<->Address relation defined in laravel as below

class Client extends Model
{
    use HasFactory;

    protected $keyType = 'string';

    public $incrementing = false;
    protected $fillable = [
        'name',
        'location',
        'status',
    ];

    public function address()
    {
        return $this->hasOne(ClientAddress::class);
    }
}
class ClientAddress extends Model
{
    use HasFactory;
    public $timestamps = false;
    
    protected $keyType = 'string';

    public $incrementing = false;
    
    protected $fillable = [
        'street',
        'zip',
    ];

    public function client()
    {
        return $this->belongsTo(Client::class);
    }
}

I am able to save it in database. When I read it like below and print I get address

public function getAddress(Request $request)
    {
        $client=Client::with('address')->find($request->id);
        Log::info($client);
        Log::info($client->address);
        return ClientAddressResource::make($client->address);
    }

first log value is :

[2024-06-22 10:39:15] local.INFO: {"id":"12cf02ff-fff7-4b8d-8eb5-64d5328afc8b","name":"Client With address","location":"Delhi","status":"OK","address":{"id":"0b956710-3681-4042-96c2-877345167c46","street":"str1","street2":"str2","city":"some city","state":"stt","country":"India","zip":"1234","client_id":"12cf02ff-fff7-4b8d-8eb5-64d5328afc8b"},"created_at":"2024-06-20T21:14:50.000000Z","updated_at":"2024-06-20T21:14:50.000000Z"}

second log is empty

[2024-06-22 10:39:15] local.INFO:

last line gives error

[2024-06-22 10:39:15] local.ERROR: Attempt to read property "street" on null {"userId":"981cfca3-46ed-47a7-91df-5424b563e723","exception":"[object] (ErrorException(code: 0): Attempt to read property "street" on null at ....

I tried a lot of google, and SO answers but none talks about reading of object and convert to json.

migrations:

Schema::create('clients', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('name');
            $table->string('location');
            $table->string('status');
     });

        Schema::create('client_addresses', function (Blueprint $table){
            $table->uuid('id')->primary();
            $table->string('street');
            $table->string('zip');
            $table->foreignUuid('client_id')->constrained();
        });
0 likes
6 replies
Vadim_K's avatar

Check that address is saved correctly. Looks likes problem with that.

rachnasundriyal33's avatar

@Vadim_K Address is saved correctly as you can see in the first log line, address is available in log but not identified by JsonResource

Ben Taylor's avatar

What happens if you return this instead?

       return new ClientAddressResource($client->address);
rachnasundriyal33's avatar

@Ben Taylor it gives error, I had to return address separately using below

$client=Client::with('address')->find($request->id); return ClientAddressResource::make($client->address()->first());

ajn123's avatar

do you need to load it ?


        return ClientAddressResource::make($client::load('address')->address);
rachnasundriyal33's avatar

@ajn123 don't know, but load is kinda lazy loading, while here I want to do eager loading. Also, as log shows data of address is correctly loaded in model without any additional call, its just that it is not available in json resource.

Clearly log::info is printing so toJson is working fine of Jasonable on Model.

Please or to participate in this conversation.