diff --git a/app/JsonApi/MorphLegends/Adapter.php b/app/JsonApi/MorphLegends/Adapter.php new file mode 100644 index 0000000..ac700d3 --- /dev/null +++ b/app/JsonApi/MorphLegends/Adapter.php @@ -0,0 +1,53 @@ +filterWithScopes($query, $filters); + } + + /** + * @return \CloudCreativity\LaravelJsonApi\Eloquent\HasMany + */ + protected function words() + { + return $this->hasMany(); + } +} diff --git a/app/JsonApi/MorphLegends/Schema.php b/app/JsonApi/MorphLegends/Schema.php new file mode 100644 index 0000000..e6926b2 --- /dev/null +++ b/app/JsonApi/MorphLegends/Schema.php @@ -0,0 +1,58 @@ + + * Date: 4/11/21 + * Time: 12:28 PM + */ +class Schema extends SchemaProvider +{ + /** + * @var string + */ + protected $resourceType = 'morph-legends'; + + /** + * @param \App\MorphLegend $resource + * the domain record being serialized. + * @return string + */ + public function getId($resource) + { + return (string)$resource->getRouteKey(); + } + + /** + * @param \App\MorphLegend $resource + * the domain record being serialized. + * @return array + */ + public function getAttributes($resource) + { + return [ + 'morph' => $resource->morph, + 'description' => $resource->description, + ]; + } + + /** + * @param object $resource + * @param bool $isPrimary + * @param array $includeRelationships + * @return array|\bool[][] + */ + public function getRelationships($resource, $isPrimary, array $includeRelationships) + { + return [ + 'words' => [ + self::SHOW_SELF => true, + self::SHOW_RELATED => true, + ] + ]; + } +} diff --git a/app/JsonApi/Words/Adapter.php b/app/JsonApi/Words/Adapter.php index ac2ff6b..72e2031 100644 --- a/app/JsonApi/Words/Adapter.php +++ b/app/JsonApi/Words/Adapter.php @@ -59,4 +59,12 @@ class Adapter extends AbstractAdapter { return $this->belongsTo(); } + + /** + * @return \CloudCreativity\LaravelJsonApi\Eloquent\BelongsTo + */ + protected function morphLegend() + { + return $this->belongsTo(); + } } diff --git a/app/JsonApi/Words/Schema.php b/app/JsonApi/Words/Schema.php index 029faa7..c0b6178 100644 --- a/app/JsonApi/Words/Schema.php +++ b/app/JsonApi/Words/Schema.php @@ -62,7 +62,15 @@ class Schema extends SchemaProvider self::DATA => function () use ($resource) { return $resource->lexical_entry; } - ] + ], + 'morph-legends' => [ + self::SHOW_SELF => true, + self::SHOW_RELATED => true, + self::SHOW_DATA => isset($includeRelationships['morph-legends']), + self::DATA => function () use ($resource) { + return $resource->morph_legend; + } + ], ]; } } diff --git a/app/MorphLegend.php b/app/MorphLegend.php new file mode 100644 index 0000000..dbdc5f8 --- /dev/null +++ b/app/MorphLegend.php @@ -0,0 +1,27 @@ +hasMany(Word::class, 'morph', 'morph'); + } +} diff --git a/app/Word.php b/app/Word.php index 4771d60..0abd7ed 100644 --- a/app/Word.php +++ b/app/Word.php @@ -42,4 +42,12 @@ class Word extends Model { return $this->belongsTo(LexicalEntry::class, 'strongs_number', 'id'); } + + /** + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function morphLegend() + { + return $this->belongsTo(MorphLegend::class, 'morph', 'morph)'); + } } diff --git a/config/json-api-default.php b/config/json-api-default.php index 3c0889d..f30add9 100644 --- a/config/json-api-default.php +++ b/config/json-api-default.php @@ -71,6 +71,7 @@ return [ 'verses' => \App\Verse::class, 'words' => \App\Word::class, 'lexical-entries' => \App\LexicalEntry::class, + 'morph-legends' => \App\MorphLegend::class, ], /* diff --git a/routes/api.php b/routes/api.php index 54ae8a0..c255615 100644 --- a/routes/api.php +++ b/routes/api.php @@ -37,9 +37,14 @@ JsonApi::register('default')->routes(function ($api) { $api->resource('words')->readOnly()->relationships(function ($relations) { $relations->hasOne('lexical-entry')->readOnly(); + $relations->hasOne('morph-legend')->readOnly(); }); $api->resource('lexical-entries')->readOnly()->relationships(function ($relations) { $relations->hasMany('words')->readOnly(); }); + + $api->resource('morph-legends')->readOnly()->relationships(function ($relations) { + $relations->hasMany('words')->readOnly(); + }); });