8000 Question regarding mapping JSON API attribute field names to model keys · Issue #519 · cloudcreativity/laravel-json-api · GitHub
[go: up one dir, main page]

Skip to content
Question regarding mapping JSON API attribute field names to model keys #519
Closed
@askmrsinh

Description

@askmrsinh

Hi,

Thank you for this library.
My DB has two columns named part1_open_date and part1_close_date which are transmitted as:

public function getAttributes($resource)
    {
        return [
            
            'part1' => [
                'open_date' => $resource->part1_open_date,
                'close_date' => $resource->part1_close_date
            ],
            'created_at' => $resource->created_at->toAtomString(),
            'updated_at' => $resource->updated_at->toAtomString(),
        ];
    }
{
    "data": 
        {
            "type": "surveys",
            "attributes": {
                "part1": {
                    "open_date": "2019-08-31",
                    "close_date": "2019-12-01"
                },
                "created_at": "2020-08-26T13:18:20+00:00",
                "updated_at": "2020-08-26T13:18:20+00:00"
            }
        }
}

A GET request to the above endpoint works well (as seen).

However, I am having issues with a POST. I am not sure how to correctly refer to the JSON nested object part1.open_date and part1.close_date within the corresponding App\JsonApi\Surveys\Adapter.php; class.

There is a section on this in the docs but modifying the $attributes variable is not working as expected.

protected $attributes = [
        'part1' => [
            'open_date' => 'part1_open_date',
            'close_date' => 'part1_close_date'
        ]
    ];

I believe I am referencing the key in the wrong way.

Any help is appreciated.

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

      0