Closed Yeleup closed 2 months ago
Show your request too.
Show your request too.
class InvestmentRequest extends ResourceRequest
{
/**
* Get the validation rules for the resource.
*
* @return array
*/
public function rules(): array
{
return [
'giver' => ['required', JsonApiRule::toOne()],
'recipient' => ['required', JsonApiRule::toOne()],
'value' => ['required', 'numeric'],
'rate' => ['required', 'numeric'],
'currency' => ['required', Rule::in(['USD', 'EUR'])],
];
}
}
request
$data = [
'type' => 'investments',
'attributes' => [
'value' => 1000,
'currency' => 'EUR',
'rate' => 0.01,
],
"relationships" => [
'giver' => [
"data" => [
"type" => 'customers',
"id" => (string) $this->giver->getRouteKey(),
],
],
'recipient' => [
"data" => [
"type" => 'customers',
"id" => (string) $this->recipient->getRouteKey(),
]
],
]
];
$response = $this
->withToken($this->user->createToken('ApiToken')->plainTextToken)
->actingAs($this->user)
->jsonApi()
->expects('investments')
->withData($data)
->post('/api/v1/investments');
$response->assertStatus(Response::HTTP_CREATED);
And your schema.
And your schema.
public function fields(): array
{
return [
ID::make(),
BelongsTo::make('organization'),
BelongsTo::make('user'),
BelongsTo::make('giver'),
BelongsTo::make('recipient'),
HasMany::make('transactions'),
Number::make('value'),
Number::make('rate'),
Str::make('currency'),
Str::make('reference')->sortable(),
DateTime::make('startedAt')->sortable(),
DateTime::make('receivedAt')->sortable(),
DateTime::make('createdAt')->sortable()->readOnly(),
DateTime::make('updatedAt')->sortable()->readOnly(),
];
}
I've decided through
BelongsTo::make('giver')->type('customers')
BelongsTo::make('recipient')->type('customers'),
Yeah that looks right because of this: https://laraveljsonapi.io/docs/3.0/schemas/relationships.html#inverse-type
Please close if this is now working.
request
schema
model
When I create I get