8000 Question: How to register adapter? · Issue #139 · cloudcreativity/laravel-json-api · GitHub
[go: up one dir, main page]

Skip to content
Question: How to register adapter? #139
Closed
@earllapura

Description

@earllapura

I have the following configuration:

    'resources' => [
        //...
        'plantillas' => 'App\Plantilla',
        'salary-grades' => 'App\SalaryGrade',
    ],

and a snippet of the route:

        $api->resource('plantillas', [
            'controller' => '\App\Http\Controllers\Api\PlantillaController',
            'only'       => ['index', 'create'],
            'has-one'    => 'salary-grade',
        ]);

I included the relationship in Plantillas\Hydrator

    protected $relationships = [
        'salary-grade',
    ];

And the relationship in Plantillas\Schema

    public function getRelationships($resource, $isPrimary, array $includeRelationships)
    {
        if (!$resource instanceof Plantilla) {
            throw new RuntimeException('Expecting a plantilla model.');
        }

        return [
            'salary-grade' => [
                self::SHOW_SELF => true,
                self::SHOW_RELATED => true,
                self::DATA => isset($includeRelationships['salary-grade']) ?
                    $resource->salaryGrade : $this->createBelongsToIdentity($resource, 'salaryGrade'),
            ],
        ];
    }

However, when I send a POST to plantilla route (using Behat) with the data as follows:

      {
          "data":
          {
              "type": "plantilla",
              "attributes": {
                  "code": "CMP Tech 1"
              },
              "relationships": {
                  "salary-grade": {
                      "data": {
                          "type": "salary-grade",
                          "id": "1"
                      }
                  }
              }
          }
      }

I get the error that says: CloudCreativity\JsonApi\Exceptions\RuntimeException: No adapter for resource type: salary-grade in file C:\xampp\htdocs\path-to-project\vendor\cloudcreativity\json-api\src\Store\Store.php on line 137

Do I have to register the adaptors manually? I already have the Adapter, Schema and Hydrator for SalaryGrades:

//App\JsonApi\Session\SalaryGrades
    /**
     * @var array
     */
    protected $attributes = ['level', 'amount'];

    /**
     * @var array
     */
    protected $relationships = [
        'plantillas',
    ];
//App\JsonApi\Session\SalaryGrades\Adapter
    /**
     * Adapter constructor.
     *
     * @param StandardStrategy $paging
     */
    public function __construct(StandardStrategy $paging)
    {
        parent::__construct(new SalaryGrade(), $paging);
    }

    /**
     * @inheritdoc
     */
    protected function filter(Builder $builder, Collection $filters)
    {
        //
    }

    /**
     * @inheritdoc
     */
    protected function isSearchOne(Collection $filters)
    {
        return $filters->has('code');
    }

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

      0