laravel: Resource ID with value zero throws the exception: No JSON API resource id name set on route

Trying to get a resource which ID value is zero, kinda similar to an issue found at the former cloudcreativity/laravel-json-api throws the LogicException No JSON API resource id name set on route. Turns out that on funtion modelOrResourceId in Route.php an assingnmet within the if stament is performed and it throws the exception because a logic value false is assumed, instead of the actual 0.

Here’s referenced code (at line 102):

    /**
     * @inheritDoc
     */
    public function modelOrResourceId()
    {
        if (!$name = $this->resourceIdName()) {
            throw new LogicException('No JSON API resource id name set on route.');
        }

        if ($modelOrResourceId = $this->route->parameter($name)) {
            return $modelOrResourceId;
        }

        throw new LogicException('No JSON API resource id set on route.');
    }

And this is the workaround the I’ve implemented in order to prevent the exception being thrown:

    /**
     * @inheritDoc
     */
    public function modelOrResourceId()
    {
        if (!$name = $this->resourceIdName()) {
            throw new LogicException('No JSON API resource id name set on route.');
        }

        if (!is_null($this->route->parameter($name)) && $this->route->parameter($name)!='') {
            return $this->route->parameter($name);
        }

        throw new LogicException('No JSON API resource id set on route.');
    }

I’m not sure if this is the best solution but, considering that it’s plausible to have a resource ID = zero, it solves my issues without affecting any other values.

Hope it helps!

About this issue

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

Most upvoted comments

Great, thanks for reporting glad it’s now fixed!

I’ve just tagged the packages that have fixes in them. Please can you do the following to upgrade:

composer require laravel-json-api/laravel --no-update
composer up laravel-json-api/*

Then if it fail please provide the actual JSON:API error or exception that it is failing with? I need that to know what’s failing!

Sorry, just twigged that you also mentioned the ResourceObject class… thanks for mentioning that, looking at the code I need to put a fix in there too.