laravel-orion: Includes are broken in version 2.17.0

Since version 2.17.0 our “include” with a relation is broken. In version 2.16.0 this still works.

My test case:

$this->post('api/attachments/search?include=chatMessage,chatMessage.to,chatMessage.from')->assertOk();

This used to work, but now it gives an 500 status. It now throws an exception, while this works in the previous version. The exception:

{#4430
  +"message": "Call to undefined method App\Models\Media::chatMessage.to()"
  +"exception": "BadMethodCallException"
  +"file": "<path>/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php"
... etc.

My Controller looks like this:

class AttachmentsController extends Controller
{
    protected $model = Media::class;

    public function includes(): array
    {
        return ['chatMessage', 'chatMessage.*'];
    }
    
    // ... etc
}

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 22 (8 by maintainers)

Most upvoted comments

I see that in the file src/Drivers/Standard/RelationsResolver.php the requestedRelations method does not include the relation if it is posted.

Changing line 44:

$requestedIncludesQuery = collect(explode(',', $request->query('include', '')));

To:

$requestedIncludesQuery = collect(explode(',', $request->get('include', '')));

Seems to resolve this issue.

I see that this is exactly the same as @jeroenqui described here 🙈

In my opinion this logic should not be broken, even if it’s an undocumented feature.

@alexzarbn When I use the “post” version of includes:

Endpoint: /api/tickets/search

includes: [
    { relation: 'customer' },
    { relation: 'user' }
]

Error 500 Call to undefined method App\\Models\\Customer::user()

Even when I try this in my controller:

public function alwaysIncludes(): array
    {
        return [
            'customer',
            'user'
        ];
    }

relations are not included.

Published a fix in v2.17.1. Could you please let me know, if the problem is resolved for all of your apps?

The following code in src/Drivers/Standard/QueryBuilder

    /**
     * Get the model class from a given relation.
     *
     * @param string $relation
     * @return string
     */
    public function getRelationModelClass(string $relation): string
    {
        return get_class((new $this->resourceModelClass)->$relation()->getModel());
    }

can’t handle nested relations like chatMessage.to