Sevendays-Digital/filament-nested-resources

Route missing parameter

almooradi-dev opened this issue ยท 3 comments

Hello,

I have 2 resources:

  • Course -> Parent
  • Session -> Nested (Child)

And the models of these resources are in a subdirectory App/Models/Academic/....
So when trying to get the child URL using ChildResourceLink::make(...) I got Missing parameter: academic/course

So I trace your code's functions:

  1. Go to NestedResource::getUrl()
    And check what the final $params is giving me, and I found this

    array:2 [โ–ผ // vendor\sevendays-digital\filament-nested-resources\src\NestedResource.php:72
      "academic\Course" => 12
      "academic/course" => null
    ]
    

    The first one academic\Course is from the first $params variable, and the second one academic/course is from the $list variable

  2. Go to ChildResourceLink::getUrl() (where the function in step 1 is called)

    $param = Str::of($this->record::class)
        ->afterLast('\\Models\\')
        ->camel();
    

    As you can see, this logic will be working perfectly for Models that are directly under the App\Models directory (and not in a subdirectory)

So, is there a solution to this from my side, or we should edit the function in step 2?

I solve it (but got a different error ๐Ÿ˜…, I am trying to figure it out)

But for the main issue above, I solve it by editing the ChildResourceLink::getUrl()

    public function getUrl(): ?string
    {
        $baseParams = [];
        if (property_exists($this->table->getLivewire(), 'urlParameters')) {
            $baseParams = $this->table->getLivewire()->urlParameters;
        }

        $param = Str::camel(Str::singular($this->resourceClass::getParent()::getSlug())); // ๐Ÿ‘ˆ Here
        // Which is the same used in "NestedResource::getParentTree()" in the "urlPlaceholder" parameter

        return $this->resourceClass::getUrl(
            'index',
            [...$baseParams, $param => $this->record->getKey()] // ๐Ÿ‘ˆ Here
        );
    }

The second issue, is related to that one (because of the models in a subdirectory)
In the NestedResource::getEloquentQuery(), when calling the static::getParentAccessor() in the whereHas we got academic\Course which is wrong of course, and it should only be course

So my nested resource I just add this:

    public static function getParentAccessor(): string
    {
        return 'course';
    }

EDIT

Don't do this
Because it will affect other places
So I override the getEloquentQuery() in my nested resource

	public static function getEloquentQuery(string|int|null $parent = null): Builder
    {
        $query = static::getModel()::query(); // ๐Ÿ‘ˆ Change here
        $parentModel = static::getParent()::getModel();
        $key = (new $parentModel)->getKeyName();
        $query->whereHas(
            'course', // ๐Ÿ‘ˆ Change here
            fn (Builder $builder) => $builder->where($key, '=', $parent ?? static::getParentId())
        );

        return $query;
    }

The second issue, is related to that one (because of the models in a subdirectory) In the NestedResource::getEloquentQuery(), when calling the static::getParentAccessor() in the whereHas we got academic\Course which is wrong of course, and it should only be course

So my nested resource I just add this:

    public static function getParentAccessor(): string
    {
        return 'course';
    }

EDIT

Don't do this Because it will affect other places So I override the getEloquentQuery() in my nested resource

	public static function getEloquentQuery(string|int|null $parent = null): Builder
    {
        $query = static::getModel()::query(); // ๐Ÿ‘ˆ Change here
        $parentModel = static::getParent()::getModel();
        $key = (new $parentModel)->getKeyName();
        $query->whereHas(
            'course', // ๐Ÿ‘ˆ Change here
            fn (Builder $builder) => $builder->where($key, '=', $parent ?? static::getParentId())
        );

        return $query;
    }

I create pull request thanks

https://github.com/Sevendays-Digital/filament-nested-resources/pull/6