CristalTeam / php-api-wrapper

:rainbow: Work with APIs like with Laravel Eloquent or Doctrine (no longer a dream)
MIT License
117 stars 32 forks source link

[HasMany] Relation returns null instead of empty array on non existing Model #15

Closed TZK- closed 3 years ago

TZK- commented 3 years ago

If we have an entity defined such as below:

<?php

namespace App\Models;

use App\ApiModel;
use Illuminate\Support\Collection;

class User extends ApiModel
{
    protected $entity = 'user';

    protected $primaryKey = 'user_id';

    public function contacts()
    {
        return $this->hasMany(Contact::class, 'user_id');
    }

   // ...

If we want to access contacts relation on newly created instance, it will return null instead of empty array (or empty collection in the scope of Laravel).

$entity = new User();
dd($entity->contacts); // null