Skip to content

How to attach a middleware to a specific resource relationship? #630

Description

@swichers

Given something like:

$api->resource('foo')
  ->relationships(static function (RelationshipsRegistration $relations) {
    $relations->hasOne('bar');
    $relations->hasOne('fizz');
    $relations->hasMany('buzz');
  })
  ->middleware('example');

How can I specify a middleware for the buzz relationship independent of bar and fizz (and the main resource)? Something akin to this is what I'm looking to do:

$api->resource('foo')
  ->relationships(static function (RelationshipsRegistration $relations) {
    $relations->hasOne('bar');
    $relations->hasOne('fizz')->middleware('cache.headers:public;max_age=604800');
    $relations->hasMany('buzz');
  })
  ->middleware('cache.headers:public;max_age=600');

On that same topic, is there a way to setup different middleware per resource endpoint? I'd like to be able to target the index separately from the individual item. I have a workaround where I split out the resource into multiple definitions using ->only('index') but it seems clunky.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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