F
Filament7mo ago
ericmp

modifyQueryUsing breaks ToggleColumn

in the app/Filament/Resources/FormResource.php file i set up the table:
public static function table(Table $table): Table
{
return $table
->modifyQueryUsing(function (Builder $query) {
$query->when(! auth()->user()->isAdmin(), function ($query) {
$query->whereHas('users', function ($query) {
$query->where('users.id', auth()->id());
});
$query->orWhere('created_by_id', auth()->id());
});
})
->columns([
Tables\Columns\ToggleColumn::make('is_active')
...
public static function table(Table $table): Table
{
return $table
->modifyQueryUsing(function (Builder $query) {
$query->when(! auth()->user()->isAdmin(), function ($query) {
$query->whereHas('users', function ($query) {
$query->where('users.id', auth()->id());
});
$query->orWhere('created_by_id', auth()->id());
});
})
->columns([
Tables\Columns\ToggleColumn::make('is_active')
...
if the user is not admin, i want to show their created forms or the forms they are attached to. the issue is that when i modify the query, if the user is admin, works just fine, but if the user is not admin, if the user toggles the is_active column, it toggles the last row being shown in the table! https://filamentphp.com/docs/3.x/panels/resources/listing-records#customizing-the-table-eloquent-query i think the issue is related to the or. why? because if i change the orWhere to a where, the toggle works fine, but then the query is not the query i want. im doing something wrong or it's actually a bug? update: i also tried it this way. this way makes the toggles to not change
->query(function (Builder $query) {
return static::getEloquentQuery()->when(! auth()->user()->isAdmin(), function ($query) {
$query->whereHas('users', function ($query) {
$query->where('users.id', auth()->id());
});
$query->orWhere('created_by_id', auth()->id());
});
})
->query(function (Builder $query) {
return static::getEloquentQuery()->when(! auth()->user()->isAdmin(), function ($query) {
$query->whereHas('users', function ($query) {
$query->where('users.id', auth()->id());
});
$query->orWhere('created_by_id', auth()->id());
});
})
36 Replies
ericmp
ericmpOP6mo ago
bump please let me know how should i modify the question so anyone can answer it xd bump
Dennis Koch
Dennis Koch6mo ago
To me it sounds like the id from users table overwrite the id of your resource and therefore it toggles the wrong one. Can you check the resulting SQL query via Debugbar or similar? Maybe a ->select('your_table.*') helps?
ericmp
ericmpOP6mo ago
sure, 1sec
ericmp
ericmpOP6mo ago
this is what i tried. i captured what queries are done when i toggle the field i did it on a record with id 15 but it updated it on the record with id 10 idk if u meant to check this
No description
Dennis Koch
Dennis Koch6mo ago
Can you check the query that loads the table
ericmp
ericmpOP6mo ago
there is a bunch, not sure if is this one
No description
ericmp
ericmpOP6mo ago
select `bgforms`.`forms`.*, (select count(*) from `bgforms`.`responses` where `bgforms`.`forms`.`id` = `bgforms`.`responses`.`form_id` and `bgforms`.`responses`.`deleted_at` is null) as `responses_count`, (select count(*) from `bgforms`.`responses` where `bgforms`.`forms`.`id` = `bgforms`.`responses`.`form_id` and `responses`.`is_read` = 0 and `bgforms`.`responses`.`deleted_at` is null) as `unread_responses_count` from `bgforms`.`forms` where (exists (select * from `bgid`.`users` inner join `bgforms`.`form_user` on `bgid`.`users`.`id` = `bgforms`.`form_user`.`user_id` where `bgforms`.`forms`.`id` = `bgforms`.`form_user`.`form_id` and `users`.`id` = 5 and `bgid`.`users`.`deleted_at` is null) or `created_by_id` = 5) and `bgforms`.`forms`.`deleted_at` is null order by `created_at` desc limit 10 offset 0
select `bgforms`.`forms`.*, (select count(*) from `bgforms`.`responses` where `bgforms`.`forms`.`id` = `bgforms`.`responses`.`form_id` and `bgforms`.`responses`.`deleted_at` is null) as `responses_count`, (select count(*) from `bgforms`.`responses` where `bgforms`.`forms`.`id` = `bgforms`.`responses`.`form_id` and `responses`.`is_read` = 0 and `bgforms`.`responses`.`deleted_at` is null) as `unread_responses_count` from `bgforms`.`forms` where (exists (select * from `bgid`.`users` inner join `bgforms`.`form_user` on `bgid`.`users`.`id` = `bgforms`.`form_user`.`user_id` where `bgforms`.`forms`.`id` = `bgforms`.`form_user`.`form_id` and `users`.`id` = 5 and `bgid`.`users`.`deleted_at` is null) or `created_by_id` = 5) and `bgforms`.`forms`.`deleted_at` is null order by `created_at` desc limit 10 offset 0
Dennis Koch
Dennis Koch6mo ago
I am not sure whether the columns from the subquery should leak to the main query, but it feels like they do. If you run that query from a DB-Tool is the form ID overwritten by the users.id?
ericmp
ericmpOP6mo ago
it is not overwritten:
No description
Dennis Koch
Dennis Koch6mo ago
Hm, weird.
ericmp
ericmpOP6mo ago
what would u do now? idk what to debug more xD
Dennis Koch
Dennis Koch6mo ago
Turn on Xdebug to check why it's resolving a wrong ID 😅 Does it only happen on the column or also for the actions?
ericmp
ericmpOP6mo ago
i think only on the toggle column uff, no clue how to do that, but i guess ill figure it out
Dennis Koch
Dennis Koch6mo ago
But it feels weird to me if it's only the column 🤔
ericmp
ericmpOP6mo ago
users use it everyday and they havent told me about issues with the row actions just the toggle
Tetracyclic
Tetracyclic6mo ago
You're about to unlock the secret weapon of fixing things quickly. What IDE are you using, and what development environment? (Sail/Herd/Valet/DDEV etc)
ericmp
ericmpOP6mo ago
vscode + herd macos secret weapon lets go 😋
ericmp
ericmpOP6mo ago
i meant, i heard about xdebug but idk why i thought it was not for vscode, like only for phpstorm (idk why xd)
Tetracyclic
Tetracyclic6mo ago
It's straightforward to set up, and once it is set up, it's so powerful for finding out what's actually happening in your application.
ericmp
ericmpOP6mo ago
thanks 🙏 ill set it up and ill let u know what i find in the table 🔍 🔍 🔍 🔍
Tetracyclic
Tetracyclic6mo ago
No description
ericmp
ericmpOP6mo ago
oh yeah i used that with java like years ago
Tetracyclic
Tetracyclic6mo ago
Click to the left of a line number to set a breakpoint, where the code will stop executing, then you can manually "step into", which will execute line by line going into any functions and diving deep into the framework, or "step over" which will jump to the next instruction at the same level you're already at.
ericmp
ericmpOP6mo ago
yesss 🙏 @Tetracyclic i followed the steps. now im on the web.php route file & start the debugger but i get:
Fatal error: Uncaught Error: Class "Illuminate\Support\Facades\Route" not found in /Users/eric/Documents/projects/bgforms/routes/web.php:17
Fatal error: Uncaught Error: Class "Illuminate\Support\Facades\Route" not found in /Users/eric/Documents/projects/bgforms/routes/web.php:17
not sure why, ill keep investigating. i had to install the vscode extension to debug aswell, not sure if it just will work or i have to configure it aswell
Tetracyclic
Tetracyclic6mo ago
ericmp
ericmpOP6mo ago
ohh, why?
Tetracyclic
Tetracyclic6mo ago
Running it from within VSCode won't (without a lot of configuration) emulate the browser environment With one of those extensions you just browse to the page that will trigger your breakpoints and it will communicate with the IDE to start debugging.
ericmp
ericmpOP6mo ago
then here i dont need a vscode file, no? or i need it anyways?
Tetracyclic
Tetracyclic6mo ago
You still need that configuration in VSCode, I believe. It tells VScode how to listen for the debug session
ericmp
ericmpOP6mo ago
u sure is available for vscode? i installed it, enabled it and went to a test route that route has 2 breakpoints but does nothing, im missing something for sure
Tetracyclic
Tetracyclic6mo ago
I think in Xdebug Helper you'll need to configure the IDE key to be "VSCODE"
Tetracyclic
Tetracyclic6mo ago
No description
Tetracyclic
Tetracyclic6mo ago
(I'm only familiar with using it in PHPStorm, so I'm guessing a bit here) Oh, apparently that isn't needed:
Tetracyclic
Tetracyclic6mo ago
Tom
Tom McFarlin
Xdebug in Visual Studio Code | Tom McFarlin
Everything that needs to be done to install Xdebug with a Homebrew-based environment and to work with the software within Visual Studio Code.
ericmp
ericmpOP6mo ago
i have no clue why my vscode button "create a launch.json file" doesnt do anything at all when clicked
Want results from more Discord servers?
Add your server