CristalTeam/php-api-wrapper

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

TZK- opened this issue · 0 comments

TZK- commented

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