Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't associate model to one which is creating #48

Closed
heyiammushu opened this issue Mar 11, 2021 · 4 comments
Closed

Can't associate model to one which is creating #48

heyiammushu opened this issue Mar 11, 2021 · 4 comments

Comments

@heyiammushu
Copy link

heyiammushu commented Mar 11, 2021

Using approach to test my create resource with relation route like here https://laraveljsonapi.io/docs/1.0/testing/resources.html#store-aka-create-testing
But unfortunately model wasn't associated, no error, just null instead of relation? Can you check this, can't figure it out, is it my mistake somewhere or it's a bug.

Worker model
public function schedules()
{
    return $this->hasMany(WorkerSchedule::class);
}
Worker Schedule model
public function worker()
{
    return $this->belongsTo(Worker::class);
}
Worker Schedules schema
public function fields(): array
{
...
    BelongsTo::make('worker'),
...
}
Worker Schedules resource
public function relationships($request): iterable
{
    return [
        $this->relation('worker')
    ];
}
Request inside the test
$response = $this
            ->jsonApi()
            ->expects('worker-schedules')
            ->withData([
                'type' => 'worker-schedules',
                'attributes' => $data,
                'relationships' => [
                    'worker' => [
                        'data' => [
                            'type' => 'workers',
                            'id' => (string) $worker->id,
                        ]
                    ],
                ],
            ])
            ->post('/api/v1/worker-schedules');
Result
Failed asserting that a row in the table [worker_schedules] matches the attributes {
    "id": "84",
    "worker_id": "77",
...
}.

Found similar results: [
    {
        "id": 84,
        "worker_id": null,
...
    }
].
@lindyhopchris
Copy link
Contributor

Have you added a rule for the relationship in the request class? We only fill the model with validated data, so if you don't validate the relationship field it won't be filled.

@lindyhopchris
Copy link
Contributor

Also, this is not correct on the worker schema:

public function worker()
{
    return $this->belongsTo(Worker::class);
}

Not sure what that is (possible incorrect copy and paste?), as this should be used on a schema:
https://laraveljsonapi.io/docs/1.0/schemas/relationships.html#belongs-to

@heyiammushu
Copy link
Author

Also, this is not correct on the worker schema:

public function worker()
{
    return $this->belongsTo(Worker::class);
}

Not sure what that is (possible incorrect copy and paste?), as this should be used on a schema:
https://laraveljsonapi.io/docs/1.0/schemas/relationships.html#belongs-to

Yeah, my bad, edited my message with the right one.

@heyiammushu
Copy link
Author

Also, this is not correct on the worker schema:

public function worker()
{
    return $this->belongsTo(Worker::class);
}

Not sure what that is (possible incorrect copy and paste?), as this should be used on a schema:
https://laraveljsonapi.io/docs/1.0/schemas/relationships.html#belongs-to

True, forgot about this, everything works now, after I added the relation rule to the request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants