Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

piljac1's avatar
Level 28

Dynamic value doesn't seem like a valid parameter for a relatable query

Hello ! Here I come again with a strange behavior. I'm trying to use the relatable query Nova feature to restrict the colors (from my Color model) shown in my BelongsTo dropdown, but I struck a huge... bug ?

First of all, here's a code snippet pulled from Nova's documentation of what I'm trying to accomplish :

public static function relatableTags(NovaRequest $request, $query)
{
    $resource = $request->route('resource'); // Returns the resource type.
    $resourceId = $request->route('resourceId'); // Returns the resource id.

    return $query->where('type', $resource);
}

Here's my relatable query :

public static function relatableColors(NovaRequest $request, $query)
{
    $resourceId = $request->route('resourceId');

    return $query->whereHas('zones', function($query) use ($resourceId) {
        $query->where('zone_id', $resourceId);
    });
}

So here's the issue : I'm supposed to receive all colors with a zones relation having the current zone ID (contained in $request->route('resourceId')), but no color option is found inside the select.

In my debug case, $resourceId contained a string of "2" (inside the subquery). I told myself that maybe I would get something by casting it to an int... nothing. I then replaced it with an hardcoded 2 (int). Success ! Wait what ? I just casted a string of "2" to an int and it didn't work beforehand. Why ?

I pushed my debug case further. I started by hardcoding a string of "2" instead of an integer, inside a variable placed outside of my subquery. It also worked. Same thing when I casted my hardcoded "2" string to float. Weird...

I then pushed it to an other level (an ugly level if you ask me). I created an anonymous function variable which returned the int casted value of $request->route('resourceId'). No success. I then changed the anonymous function to increment a variable with the help of a loop while the variable was smaller than $request->route('resourceId') (it's big brain time !!!). No success again...

I also tried integer multiplications, additions and substractions on my $request->route('resourceId') variable. No luck...

Can someone give me explanations ? I'm kinda out of ideas.

0 likes
3 replies
piljac1's avatar
Level 28

@sinnbeck Thanks for the suggestion. I'll ask the project admin if he wants to upgrade.

As a side note, I pin pointed the source of the issue, but have not pushed my research further (I'm an employee and time is money of course). I will however try to debug further on my personal time.

So here's the cause : the relatableColors function is called twice. The first time (where my first tests ended because I was dumping the $request->route('resourceId')assuming it should never change because we are in a zone edit view), it returns the actual resource ID (zone ID) we're editing. The second time, the resource ID is empty, so this is why the problem I encountered is happening.

piljac1's avatar
Level 28

Update #2

I'm getting a different "type" of $request objects on both relatableColorscalls

[2019-11-14 16:16:11] local.DEBUG: array (
  'editing' => 'true',
  'editMode' => 'update',
  'viaResource' => NULL,
  'viaResourceId' => NULL,
  'viaRelationship' => NULL,
)  
[2019-11-14 16:16:12] local.DEBUG: array (
  'current' => '2',
  'first' => 'false',
  'search' => NULL,
  'withTrashed' => 'false',
  'resourceId' => '2',
  'viaResource' => NULL,
  'viaResourceId' => NULL,
  'viaRelationship' => NULL,
)  

Please or to participate in this conversation.