Skip to content

Filling morphMany relationships #615

@estuardoquan

Description

@estuardoquan

not really an issue but, currently I have:

Articles (Schema):
'title' => string,
'slug' => string,
'content' => string,
'user_id' => foreignId,

Images (Schema):
'url' => string,
'imageable_id' => string,
'imageable_type' => string,
OR
morphs(imageable),

Article (Class):
user = () => {
return $this->belongsTo(User::Class);
}
images = () => {
return $this->morphMany(Image::class, 'imageable');
}

Image (Class):
imageable = () => {
return $this->morphTo('imageable');
}

and im testing, on php unit;
$this->jsonApi()->withData([ 'type' => 'articles', 'attributes' => Article::factory()->raw(), 'relationships' => [ 'users' => [ 'data' => [ 'type' => 'users', 'id' => User::factory()->create()->getRouteKey(), ] ], ], ])->post(route('api.v1.articles.create));
this works; but is there a way to include the images relationship within the same request... because the only thing Im thinking about right now is that the Images table can only be filled via the images routes:
$this->jsonApi()->withData([ 'type' => 'images', 'attributes' => Image::factory()->raw(), 'relationships' => [ 'imageable' => [ 'data' => [ 'type' => 'articles', 'id' => Article::factory()->create()->getRouteKey(), ] ], ], ])->post(route('api.v1.images.create));

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