Skip to content

Singular not working on hasMany relation #198

@sneakylenny

Description

@sneakylenny

When I have 2 entities, for example: group and member:

Group
public function fields(): array
{
    return [
        ID::make(),
        Str::make('name'),
        Str::make('slug'),
        DateTime::make('created_at')->sortable()->readOnly(),
        DateTime::make('updated_at')->sortable()->readOnly(),

        // Relations
        HasMany::make('mambers')->canCount(),
    ];
}

public function filters(): array
{
    return [
        WhereIdIn::make($this),
        Where::make('slug')->singular(),
    ];
}
Member
public function fields(): array
{
    return [
        ID::make()->uuid(),
        Str::make('name'),
        Str::make('identifier'),
        Str::make('data'),
        DateTime::make('created_at')->sortable()->readOnly(),
        DateTime::make('updated_at')->sortable()->readOnly(),
        
        // Relations
        BelongsTo::make('group')
    ];
}

public function filters(): array
{
    return [
        WhereIdIn::make($this),
        Where::make('identifier')->singular(),
    ];
}

If I want to query a member of a group by it's unique and singular identifier I still get an array as if it wasn't singular.

To elaborate:

Works fine:

GET http://localhost/api/v1/members?filter[identifier]=JG5RUJUJ HTTP/1.1

Result

{
  "jsonapi": {
    "version": "1.0"
  },
  "links": {
    "self": "/api/v1/members/3a27001d-ab68-461c-ab88-f2a60066c236"
  },
  "data": {
    "type": "members",
    "id": "3a27001d-ab68-461c-ab88-f2a60066c236",
    "attributes": {
      "name": "Test",
      "identifier": "JG5RUJUJ",
      "data": null,
      "created_at": "2022-05-20T11:19:00.000000Z",
      "updated_at": "2022-05-20T11:19:00.000000Z"
    },
    "relationships": {
      "group": {
        "links": {
          "related": "/api/v1/members/3a27001d-ab68-461c-ab88-f2a60066c236/group",
          "self": "/api/v1/members/3a27001d-ab68-461c-ab88-f2a60066c236/relationships/group"
        }
      }
    },
    "links": {
      "self": "/api/v1/members/3a27001d-ab68-461c-ab88-f2a60066c236"
    }
  }
}

Doesn't return singular

GET http://localhost/api/v1/group/5b6b94e2-dfcb-4650-a9b4-bc3d1cc28cdb/members?filter[identifier]=JG5RUJUJ HTTP/1.1

Returns

{
  "meta": {
    "count": 2
  },
  "jsonapi": {
    "version": "1.0"
  },
  "links": {
    "related": "/api/v1/groups/5b6b94e2-dfcb-4650-a9b4-bc3d1cc28cdb/members",
    "self": "/api/v1/groups/5b6b94e2-dfcb-4650-a9b4-bc3d1cc28cdb/relationships/members"
  },
  "data": [  <-- returns array
    {
      "type": "members",
      "id": "3a27001d-ab68-461c-ab88-f2a60066c236",
      "attributes": {
        "name": "Test",
        "identifier": "JG5RUJUJ",
        "data": null,
        "created_at": "2022-05-20T11:19:00.000000Z",
        "updated_at": "2022-05-20T11:19:00.000000Z"
      },
      "relationships": {
        "group": {
          "links": {
            "related": "/api/v1/members/3a27001d-ab68-461c-ab88-f2a60066c236/group",
            "self": "/api/v1/members/3a27001d-ab68-461c-ab88-f2a60066c236/relationships/group"
          }
        }
      },
      "links": {
        "self": "/api/v1/members/3a27001d-ab68-461c-ab88-f2a60066c236"
      }
    }
  ]
}

I've also tried defining the filter like this:

HasMany::make('clients')->canCount()->withFilters(
    Where::make('identifier')->singular(),
),

Which returns the same as above

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions