Problem editing filtered record
Hi guys. I hope someone can help me.
I have a table with data that is filtered before loading based on the role of the logged in user. In this way, the user only sees the records that he/she must manage. The query I use is the following:
public static function getEloquentQuery(): Builder
{
if (Auth::user()->hasRole('panel_user')) {
return parent::getEloquentQuery()->where('modality', 'TF');
} elseif (Auth::user()->hasRole('panel_user_presencial')) {
return parent::getEloquentQuery()->where('modality', '=','P')->Orwhere('modality', '=', 'M')->Orwhere('modality', '=', 'AV');
} else {
return parent::getEloquentQuery();
}
} Los datos obtenidos son correctos para cada usuario, el problema ocurre cuando se intenta editar un registro, siempre edita el primero de la lista independientemente del que pulse. Why does this happen? How can I solve it? Thanks in advance
} Los datos obtenidos son correctos para cada usuario, el problema ocurre cuando se intenta editar un registro, siempre edita el primero de la lista independientemente del que pulse. Why does this happen? How can I solve it? Thanks in advance
Solution:Jump to solution
Hello
I have used the modifyQueryUsing() function on the table and the records it displays for each user are correct, however when editing a record the information it loads is still incorrect, that is, it always loads the information from the first record shown regardless of the one you select.
Example:...
5 Replies
Try to use
->modifyTableQuery()
on the table()
function in your resource. That should be the place where you add such thing.
Or you could add this as a global model scope tooHi Modestas.
Could you tell me how to do what you are telling me? I don't see the method you are telling me about in the Filament documentation.
Regards
Solution
Hello
I have used the modifyQueryUsing() function on the table and the records it displays for each user are correct, however when editing a record the information it loads is still incorrect, that is, it always loads the information from the first record shown regardless of the one you select.
Example:
->modifyQueryUsing(function (Builder $query) {
if (Auth::user()->hasRole('panel_user')) {
return $query->where('modality', 'TF');
} elseif (Auth::user()->hasRole('panel_user_presencial')) {
return $query->where('modality', '=','P')->Orwhere('modality', '=', 'M')->Orwhere('modality', '=', 'AV');
}
})
I have observed that the id of the record that is loaded for editing is correct but the information is not.
What could be the cause of this?
Regards.
That's interesting. It should not affect what is loaded, since it should go by the ID.
Do you have any other modifications on the edit page or form itself?
Hello.
It's already fixed, I had left a line of code that was doing its thing.
Thanks for the answer Modesta. It has been very helpful.
Regards.