Skip to content

Custom actions controller methods can not use model type hints when using hash ids #188

@Feng-Shui

Description

@Feng-Shui

When using the hash ids for a model as per the documentation, if you create a custom action using withId(), you can not type hint the model correctly:

class MyController extends JsonApiController
{
    /**
     * @param MyModel $model
     */
    public function customActionHandler(MyModel $model)
    {

This code will give you a 404:

        {
            "detail": "No query results for model [App\\Models\\MyModel] x6Qe6n",
            "status": "404",
            "title": "Not Found"
        }

It seems to me that somewhere in Illuminate\Routing\router::substituteImplicitBindings() it can't resolve an instance of the model based on the hash id.

If you change the type hint to mixed, you don't get the 404 and you still have the correct model passed:

public function customActionHandler(mixed $model)

I've had a look through the docs but can't see any config step that I've missed in terms of setting up hash IDs correctly, given the comments in the docs that custom actions are not likely to be needed, and hash IDs are optional, this might be an edge case when using the two.

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