jeudi 2 avril 2020

Laravel - how to add variable type to parameter of controller method

I have a controller method that has a parameter. I need to add a type to it:

public function deleteIndex(int $user_id)
    {
        return DB::try(function () use ($user_id) {
            if (empty($user_id)) {
                return error(trans('user.invalid_data_recheck'));
            }

            $user = User::find($user_id);

            if (empty($user)) {
                return error(trans('user.invalid_user'));
            }

            if (!$user->delete()) {
                return error(trans('user.could_not_delete_user'));
            }

            return success();
        });
    }

The route looks like this:

AdvancedRoute::controllers([
    'users'       => 'API\UserAPIController'
]);

From the JS the parameter is being sent like this: '/users/{user_id}'

The issue I have is, when I add the parameter type: "int" in the controller method, the endpoint is no longer reached, if I remove "int" everything works fine. But I need that for an openapi library that I use. Any help is appreciated!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire