filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
19.29k stars 2.96k forks source link

issue with modifying the query for the list resource #8647

Closed nickw-lr closed 1 year ago

nickw-lr commented 1 year ago

Package

filament/filament

Package Version

v3.0.56

Laravel Version

v10.24.0

Livewire Version

v3.0.5

PHP Version

PHP 8.2.4

Problem description

Referencing #8549

"If you update the list resource query using table() - your list view will no longer render."

The issue was closed because no repo was provided. I have created a repo to demonstrate the problem.

Expected behavior

The table view should generate.

Steps to reproduce

Run steps from the provided repo's readme.

Reproduction repository

https://github.com/nickw-lr/filament-bugreport/tree/main

Relevant log output

No response

faizananwerali commented 1 year ago

@nickw-lr why not edit directly in resource file?

PostResource.php

    public static function getEloquentQuery(): Builder
    {
        return parent::getEloquentQuery()
            ->withoutGlobalScopes([
                SoftDeletingScope::class,
            ]);
    }
nickw-lr commented 1 year ago

@faizananwerali This was just a demonstration of the issue, when following the instructions from the docs.

I have an instance in my actual project where I need to filter the list of posts that have child posts. So I have a self related relation manager for the children. I need the list page for the resource to be scoped to only show posts without a parent, so that I can sort their order with an action on the list page.

If I scope the resource itself, then I can no longer use the view page from the main resource on the child posts in the relation manager (because they are out of the global scope).

danharrin commented 1 year ago

I think this was fixed a couple of weeks ago in a different PR. Please test on the latest version, if it still doesn't work then I will reopen this issue, but I'm pretty sure it should be ok now 👍

nickw-lr commented 1 year ago

Hi @danharrin, I've updated my demo repo to filament v3.77 and the issue still appears to be there.

danharrin commented 1 year ago

Is there an error?

nickw-lr commented 1 year ago

Nothing in the Laravel log file or console log. Just an "empty" posts page.

Screenshot 2023-10-16 at 09 51 10
danharrin commented 1 year ago

ListRecords::table() is going to override the resource table(). You just need to define modifyQueryUsing() on the resource table, or move the rest of the resource table code into the ListRecords page. So the table configuration is all in one place.

This is unrelated to the query, if you make query modifications in the resource table() then they will only apply to the table. Only if you use getEloquentQuery() will it apply to the other pages too.

nickw-lr commented 1 year ago

That has done the trick, thanks for that @danharrin !

kirkbushell commented 11 months ago

The docs need to be updated - they're wrong: https://filamentphp.com/docs/3.x/panels/resources/listing-records#customizing-the-table-eloquent-query

It should be:

public function table(Table $table): Table
    {
        return parent::table($table)
            ->modifyQueryUsing(fn(Builder $query) => $query->withoutGlobalScopes());
    }
danharrin commented 11 months ago

Actually, you should just to this in the resource table() method

danharrin commented 11 months ago

I've updated the docs to reflect that