Closed askmrsinh closed 4 years ago
Both of these don't work
'part1' => [
'open_date' => 'part1_open_date',
'close_date' => 'part1_close_date'
]
local.ERROR: Illegal offset type {"exception":"[object] (ErrorException(code: 0): Illegal offset type at /home/ashesh/Workspace/Work/coe-template/vendor/cloudcreativity/laravel-json-api/src/Eloquent/Concerns/DeserializesAttributes.php:111) [stacktrace]
'part1.open_date' => 'part1_open_date',
'part1.close_date' => 'part1_close_date',
local.ERROR: Array to string conversion {"exception":"[object] (ErrorException(code: 0): Array to string conversion at /home/ashesh/Workspace/Work/coe-template/vendor/laravel/framework/src/Illuminate/Support/Str.php:449) [stacktrace]
Hi! Yeah, mapping a single JSON API field to multiple model attributes is not supported.
The way we do this at the moment is to add a setPart1Attribute()
method to the model i.e. an Eloquent mutator. That would receive the array value containing both the open_date
and the close_date
.
Thank you! That worked perfectly. I was going through the past issues and came across https://github.com/cloudcreativity/laravel-json-api/issues/360 which was fixed through https://github.com/cloudcreativity/laravel-json-api/pull/361.
I was trying to make a similar enhancement in this case, however it seems a lot more work.
Yeah dot notation for the soft delets field is simpler as the adapter is specifically looking for that field.
The thing that makes it hard for setting the attributes is the adapter loops through the fields in the JSON API resource's attributes member... which is why it'd be a lot more work to implement something that maps a single field to multiple model columns.
I'm going to close this for the moment as I don't plan to support nested value mapping. Maybe if it comes up again in the future but for the moment the solution is to use an Eloquent mutator.
Hi,
Thank you for this library. My DB has two columns named
part1_open_date
andpart1_close_date
which are transmitted as: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 objectpart1.open_date
andpart1.close_date
within the correspondingApp\JsonApi\Surveys\Adapter.php;
class.There is a section on this in the docs but modifying the
$attributes
variable is not working as expected.I believe I am referencing the key in the wrong way.
Any help is appreciated.